Web2Py - 上传文件并将内容读取为 Zip 文件 [英] Web2Py - Upload a file and read the content as Zip file

查看:31
本文介绍了Web2Py - 上传文件并将内容读取为 Zip 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 Web2Py 表单上传一个 zip 文件,然后阅读内容:

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)

出于某种原因,此代码确实有效,并抱怨文件不是 zip 文件,尽管上传的文件是 zip 文件.

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

我是 Web2Py 的新手.data 如何表示为 zip 文件?

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

推荐答案

web2py 表单字段上传已经是 cgi.FieldStorage,你可以使用:

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

data = request.vars.myfile.value

对于不需要StringIO的类文件对象,使用:

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

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

这篇关于Web2Py - 上传文件并将内容读取为 Zip 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆