【发布时间】:2016-02-25 19:14:39
【问题描述】:
我有这样的表格:
<form action="/test-upload" method="post" enctype="multipart/form-data">
<input type="file" name="upload" />
<input type="submit" name="submit" value="Start upload" />
</form>
我有这样的功能:
@route("/test-upload")
@post("/test-upload")
def test_upload():
if request.POST.get("submit"):
f = request.POST.get("upload")
upload_path = "uploaded_files/{0}".format(f.filename)
f.save(upload_path, overwrite=True)
return "ok"
return template("test_upload")
这会导致以下错误:
File "/usr/lib/python3.4/site-packages/bottle.py", line 2389, in save
self._copy_file(fp, chunk_size)
File "/usr/lib/python3.4/site-packages/bottle.py", line 2367, in _copy_file
read, write, offset = self.file.read, fp.write, self.file.tell()
ValueError: I/O operation on closed file
如果我改成这个,我会得到和上面一样的错误:
f.save("uploaded_files", overwrite=True)
如果我使用其中任何一个:
with open(upload_path, 'w') as open_file:
open_file.write(f.file.read())
或
with open(upload_path, 'wb') as open_file:
open_file.write(f.file.read())
我这个错误:
open_file.write(f.file.read())
ValueError: read of closed file
令人困惑的是,某些文件确实保存到文件系统,具有适当的扩展名(我已经测试过 jpeg 和 pdf),任何文件中都没有数据。我只是看不出我对这两个版本都做错了什么,如果有的话。我正在寻找上传包含数据的文件。
我正在使用 Python3.4 和瓶子。
我看过的一些东西:How to upload and save a file using bottle framework
【问题讨论】:
标签: file python-3.x bottle