如何使用Python文件上传接受zip文件

时间:2015-03-08 14:41:25

标签: python python-3.x

我使用Python 3接受文件上传。除了包含多个文件的压缩文件外,该系统在我使用的所有测试用例中都能正常运行。

我可以用这个来复制这个问题:

<form  enctype="multipart/form-data" method="post" action="processForm.py">
Upload file:<input id="userfile" name="userfile" type="file">
<input name="submit" value="Submit" type="submit">
</form>

提交到:

#!/usr/local/bin/python3.3

print("Content-type: text/html\n")

import cgi
# enable debugging
import cgitb
cgitb.enable()

form = cgi.FieldStorage()
fileitem=form['userfile']
if fileitem.filename:
    fn = os.path.basename(fileitem.filename)
    open('files/' + fn, 'wb').write(fileitem.file.read())
        message = 'The file "' + fn + '" was uploaded successfully'
    else:
        print('no fileitem.filename')

并尝试发送包含两个文本文件的.zip文件。

错误在form = cgi.FieldStorage()行上,如下所示:

python3.3/cgi.py in __init__(self=FieldStorage(None, None, []), fp=None, headers={'content-length': '1875', 'content-type': 'multipart/form-data; boundary=---------------------------19416681558887788815658698'}, outerboundary=b'', environ=environ({'SERVER_PORT': '80', 'DOCUMENT_ROOT': '...late', 'HTTP_ACCEPT_LANGUAGE': 'en-US,en;q=0.5'}), keep_blank_values=0, strict_parsing=0, limit=None, encoding='utf-8', errors='replace')

cgi.py in read_multi(self=FieldStorage('userfile', 'signalFreqNov14-3c.txt.zip', None), environ=environ({'SERVER_PORT': '80', 'DOCUMENT_ROOT': '...late', 'HTTP_ACCEPT_LANGUAGE': 'en-US,en;q=0.5'}), keep_blank_values=0, strict_parsing=0)

         if not valid_boundary(ib):
             raise ValueError('Invalid boundary in multipart form: %r' % (ib,))

0 个答案:

没有答案