help uploading and storing a file with web.py <type 'exceptions.IOError'>

140 views
Skip to first unread message

tna...@yahoo.com

unread,
Jan 2, 2013, 2:35:27 AM1/2/13
to we...@googlegroups.com
i need help uploading and storing files with web.py 

this is my code so far


import web

urls = (
    '/hello', 'index',
    '/hello/upload', 'upload'
)
app = web.application(urls, globals()) # handels http request  that aks for urls
# the base ells lpthw.web to use the templates/layout.html file as the base template for all the other templates
render = web.template.render('templates/', base="layout")
# must use port 127.0.0.1:5000
class index(object):
   
    def GET(self):
        return render.hello_form()
       
    def POST(self):
        form = web.input(name="Nobody", greet="Hello")
        greeting = "%s, %s" % (form.greet, form.name)
        return render.index(greeting = greeting)

class upload(object):
   
    def POST(self):
        x = web.input(files={})
       
        filedir = '/project/webtest/templates'  # directory were you want to save the file
        if 'files' in x:  # check if the file -object is created
            filepath=x.files.filename.replace('\\','/') # replace the windows -style slashes with linux ones
            filename=filepath.split('/')[-1] #splits the and chooses the last part (the filename with extension
            fout = open(filedir +'/'+ filename,'w') # creates the file where the uploaded file should be stored
            fout.write(x.files.file.read()) #writes the uploaded file to the newly created file.           
            fout.close() #closes the file
        else:
            return "Error no file"
           
           
if __name__ == "__main__":
    app = web.application(urls, globals())
    app.run()


i keep geting a

<type 'exceptions.IOError'>

help :O

Jessica Le

unread,
Jan 10, 2014, 11:09:30 PM1/10/14
to we...@googlegroups.com
I'm receiving the same error as well, and I am following the web.py cookbook to the tea. Has anyone found the solution yet?

Hugo Lol

unread,
Jan 13, 2014, 1:44:45 PM1/13/14
to we...@googlegroups.com
In which line are you getting the error? Running under Windows or Linux?

Jessica Le

unread,
Jan 15, 2014, 3:30:08 PM1/15/14
to we...@googlegroups.com
I actually kind of figured it out. I had to list the entire directory for it to work. So instead of 

filedir = '/project/webtest/templates'  # directory were you want to save the file 

I changed it to:

filedir = 'C:/project/webtest/templates'  # directory were you want to save the file 

However, I'm trying to work on saving this in memory, since saving uploaded files to the local drive is BAD BAD practice. Any suggestions/links that I could refer to? I have searched using the REST web service, but there just isn't much documentation on it. 

Thanks!

Shannon Cruey

unread,
Jan 15, 2014, 3:50:16 PM1/15/14
to we...@googlegroups.com
Here's the code of my working "upload" url.  I'm saving the file, but the value in x["fupFile"].file.read() could also be stored somewhere else, in the session, or in a variable if you intend to use it later in this request.

Also note my use of os.path.join.  It's bad practice to hardcode slashes in a path - it's not cross platform!  os.path.join will concatenate many items together using the OS specific path.

class upload:

    def GET(self):

        return """This endpoint only accepts POSTS from file_upload.html"""

    def POST(self):

        x = web.input(fupFile={}, ref_id="")

        if x:

            # print x # ref_id

            # web.debug(x['fupFile'].filename) # This is the filename

            # web.debug(x['fupFile'].value) # This is the file contents

            # web.debug(x['fupFile'].file.read()) # Or use a file(-like) object

            

            ref_id = (x.ref_id if x.ref_id else "")

            filename = "%s.tmp" % (ref_id)

            fullpath = os.path.join("tmp", filename)

            with open(fullpath, 'w') as f_out:

                if not f_out:

                    raise Exception("Unable to open %s for writing." % (fullpath))

                f_out.write(x["fupFile"].file.read())  # writes the uploaded file to the newly created file.

            

            # all done, we loop back to the file_upload.html page, but this time include

            raise web.seeother("static/pages/file_upload.html?ref_id=%s&filename=%s" % (ref_id, filename))



--
You received this message because you are subscribed to the Google Groups "web.py" group.
To unsubscribe from this group and stop receiving emails from it, send an email to webpy+un...@googlegroups.com.
To post to this group, send email to we...@googlegroups.com.
Visit this group at http://groups.google.com/group/webpy.
For more options, visit https://groups.google.com/groups/opt_out.

Reply all
Reply to author
Forward
0 new messages