I think that in order to handle POST requests you need a POST method not a
GET. However, here is a short example which worked (taken from webpy.org)
(change the filedir - btw, if you have Linux and you want to save a file to
a folder make sure you have permissions):

import web

urls = ('/upload', 'Upload',
        '/', 'Index')

class Upload:
    def GET(self):
        web.header("Content-Type","text/html; charset=utf-8")
        return """<html><head></head><body>
<form method="POST" enctype="multipart/form-data" action="/">
<input type="file" name="myfile" />
<br/>
<input type="submit" />
</form>
</body></html>"""

class Index:
    def POST(self):
        x = web.input(myfile={})
        filedir = 'C:\\' # change this to the directory you want to store
the file in.
        if 'myfile' in x: # to check if the file-object is created
            filepath=x.myfile.filename.replace('\\','/') # replaces 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.myfile.file.read()) # writes the uploaded file to
the newly created file.
            fout.close() # closes the file, upload complete.
        raise web.seeother('/upload')


if __name__ == "__main__":
   app = web.application(urls, globals())
   app.run()


On Thu, Jun 6, 2013 at 10:20 AM, Joseph Do <josephdo...@gmail.com> wrote:

> @daniel: I have index class to handle POST request.
>
> class index:
>     def GET(self):
>         return render.index()
>
>     def POST(self):
>         temp = web.input(myfile={})
>         filedir = '/home/daidq/web.py/upload/images'
>         if 'myfile' in temp:
>             filepath = temp.myfile.filename.replace('\\','/')
>             filename = filepath.split('/')[-1]
>             fout = open(filedir +'/'+ filename,'w')
>             fout.write(temp.myfile.file.read())
>             fout.close()
>         raise web.seeother('/gallery')
>
> On Thursday, June 6, 2013 11:15:12 AM UTC+7, Joseph Do wrote:
>
>> I am trying to upload images and here is my code for handling file upload:
>>
>> def POST(self):
>>>         temp = web.input(myfile={})
>>>         filedir = 
>>> '/home/daidq/web.py/upload/**images<http://web.py/upload/images>
>>> '
>>>         if 'myfile' in temp:
>>>             filepath = temp.myfile.filename.replace('**\\','/')
>>>             filename = filepath.split('/')[-1]
>>>             fout = open(filedir +'/'+ filename,'w')
>>>             fout.write(temp.myfile.file.**read())
>>>             fout.close()
>>>         raise web.seeother('/gallery')
>>>
>>
>> But the problem is that it keeps show me error: *name 'temp' is not
>> defined* at *if 'myfile' in temp:* line
>>
>> Please help me to get over this.
>>
>  --
> 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+unsubscr...@googlegroups.com.
> To post to this group, send email to webpy@googlegroups.com.
> Visit this group at http://groups.google.com/group/webpy?hl=en.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>



-- 
This message is confidential. It may also be privileged or otherwise
protected by work product immunity or other legal rules. If you have
received it by mistake please let me know by reply and then delete it from
your system; you should not copy the message or disclose its contents to
anyone.

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to webpy@googlegroups.com.
Visit this group at http://groups.google.com/group/webpy?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to