开发者

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

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开发者_如何学C be represented as zip-file?


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)


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜