Web2Py - Upload a file and read the content as Zip file

可紊 提交于 2019-12-10 00:45:42

问题


I am trying to upload a zip file from Web2Py form and then read the contents:

form = FORM(TABLE(
           TR(TD('Upload File:', INPUT(_type='file', 
                                       _name='myfile', 
                                       id='myfile', 
                                       requires=IS_NOT_EMPTY()))), 
           TR(TD(INPUT(_type='submit',_value='Submit')))
       ))

if form.accepts(request.vars):  
    data=StringIO.StringIO(request.vars.myfile)  
    import zipfile  
    zfile=zipfile.Zipfile(data)

For some reason this code does work and complains of file not being a zip file although the uploaded file is a zip file.

I am new to Web2Py. How can the data be represented as zip-file?


回答1:


web2py form field uploads already are cgi.FieldStorage, you can get the raw uploaded bytes using:

data = request.vars.myfile.value

For a file-like object StringIO is not needed, use:

filelike = request.vars.myfile.file
zip = zipfile.Zipfile(filelike)



回答2:


HTTP uploads aren't just raw binary, it's mixed-multipart-form encoded. Write request.vars.myfile out to disk and you'll see, it'll say something like

------------------BlahBlahBoundary
Content-Disposition: type="file"; name="myfile"
Content-Type: application/octet-stream

<binary data>
------------------BlahBlahBoundary--

The naive solution for this is, use cgi.FieldStorage(), the example I provide uses wsgi.input, which is part of mod_wsgi.

form = cgi.FieldStorage(fp=environ['wsgi.input'], environ=environ)
raw_filw = cStringIO.StringIO(form['myfile'].file.read())

Two things to point out here

  • Always use cStringIO if you have it, it'll be faster than StringIO

  • If you allow uploads like this, you're streaming the file into ram, so, however big the file is is how much ram you'll be using - this does NOT scale. I had to write my own custom MIME stream parser to stream files to disk through python to avoid this. But, if you're learning or this is a proof of concept you should be fine.



来源:https://stackoverflow.com/questions/6407558/web2py-upload-a-file-and-read-the-content-as-zip-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!