On Saturday, November 19, 2011 4:54:03 AM UTC-5, miroslavgojic wrote:
>
> What I have for now:
>
> in view:
> {{=form}}
> {{=form_data}}
>
No need for 'form_data' on the page (your controller doesn't return it 
anyway).
 

> in controller:
> import cgi
>
> def upload():
>     form = FORM("Upload
>
> file:",INPUT(_type='file',_name='myfile'),INPUT(_type='submit',_name='submit',_value='Submit'))
>
    if form.accepts(request,session):
>         response.flash = 'form accepted'
>     elif form.errors:
>         response.flash = 'form has errors'
>     else:
>         response.flash = 'please fill the form'
>     form_data = cgi.FieldStorage()
>
No need for 'form_data'. When you upload the file, the field values are 
stored in request.vars and then transferred into form.vars. The file upload 
will be in form.vars.myfile, which will already be a cgi.FieldStorage() 
object. So, form.vars.myfile.file will be the open file object, and 
form.vars.myfile.filename will be the original name of the uploaded file.
 

>     file_data = form['myfile']
>     fp =open('some/file','wb') // this folder/file make some errors
>
It's generally best to use os.path.join to build file paths. Also, if you 
want to store the files within your application's folder, you should start 
at request.folder, which is the absolute path to the application folder:

import os
filepath = os.path.join(request.folder, 'path', 'to', 'myfile.ext')
 

>     fp.write(file_data)
>     fp.close()
>     return dict(form=form,file_data=file_data)
>
Just return the form to the view -- handle everything else in the 
controller.
 
Anthony

>

Reply via email to