How can i implement this with web2py?

86 views
Skip to first unread message

teld dlet

unread,
May 1, 2016, 10:34:40 PM5/1/16
to web2py-users
I have a python program where I prompt the user for two files, and I do some programming and output a new file...

How can I make this work with web2py?


I was thinking having a form with 2 upload fields, and a text field so they can give a name to the file that will be generated from the code. After form submission I access the two uploaded files from form.vars and do what needs to be done, then not sure how I would output a downloadable file for the user?

None of these files need to be saved in the database for later retrieval so not sure if I should just use sqlformfactory, this is all something that would happen during their session and they can choose to save the new file to their computer.



Any advice will help, thank you


Mirek Zvolský

unread,
May 2, 2016, 6:40:11 AM5/2/16
to web2py-users
For input you could save the files temporary as upload files,

or without saving them you can use HTML5 javascript FileReader and save the file content to hidden <input>s.
No idea if web2py will upload something based on file handlers from SQLFORM.factory with upload fields.
If so, you will upload it twice (from hidden fields and as standard upload), and so you could re-create both file <input>s before the submit via javascript DOM manipulation (because it is not possible set the value of upload field from javascript, nor clear it: the 1-st is clever, but the 2nd stupid :( )

For output you could
- return the resulting file content as string from the controller (I hope web2py makes nothing with string output),
- and change http headers for download (Content-Disposition or so) to force Save dialog.

Maybe somebody has much much better answer, but previous is not too difficult, few lines of code for FileReader. FileReader is supported in modern browsers but not IE8. (IE9?)




Dne pondělí 2. května 2016 4:34:40 UTC+2 teld dlet napsal(a):

teld dlet

unread,
May 3, 2016, 5:55:09 PM5/3/16
to web2py-users
Thank you for the suggestion, I will be looking at the javascript filereader!

Using this method with javascript, if I am working with image files, do you know if this will alter the size of the file? I need the size to remain consistent, which is why I will not be able to use the provided uploads option in web2py forms.

Leonel Câmara

unread,
May 3, 2016, 7:30:32 PM5/3/16
to web2py-users
Yep web2py does nothing with string output from the controllers - it's returned as is. That said you should use response.stream for the download of the resulting file.
Reply all
Reply to author
Forward
0 new messages