简体   繁体   English

创建 ZipFile 对象后,Flask FileStorage 文件变为空

[英]Flask FileStorage file becomes empty after creating ZipFile object

I'm trying to save a .zip file received from an endpoint, but first check the contents of the .zip.我正在尝试保存从端点收到的 .zip 文件,但首先检查 .zip 的内容。

If I do:如果我做:

import zipfile
from Flask import request

file = request.files["file"]
zf = zipfile.ZipFile(file, "r")
// some operation to check zf
file.save("path/to/save/file")

The saved file is empty.保存的文件是空的。 However, removing the construction of the ZipFile object, the file saved is not empty.但是,去掉 ZipFile 对象的构造,保存的文件不为空。 Any advice on why this is the case?关于为什么会这样的任何建议?

Wanted to post the solution I found.想发布我找到的解决方案。

Adding添加

file.stream.seek(0)

to move the file pointer back to the beginning, before将文件指针移回开头,之前

file.save("path/to/save/file")

solves it.解决它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM