Python ValueError: I/O operation on closed file, files saves with no data

Python ValueError: I/O operation on closed file, files saves with no data

我有这样的表格:

<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

和:http://bottlepy.org/docs/dev/tutorial.html#file-uploads

尝试

f = request.files.get('upload') # .files, not .post
f.save(upload_path)

编辑: #567 In python 3.4 multipart file upload breaks due to change in cgi.FieldStorage

但它应该被修复。 你可以尝试将你的瓶子版本更新到最后一个吗?