Python File 对象到 Flask 的 FileStorage

2024-03-25

我正在尝试在 Flask 中测试我的 upload() 方法。唯一的问题是文件存储Flask 中的对象有一个方法 save() ,PythonFile对象没有。

我像这样创建我的文件:

file = open('documents-test/test.pdf')

但我无法测试我的 upload() 方法,因为该方法使用 save()。

有什么想法如何将此 File 对象转换为 Flask Filestorage 对象吗?


http://werkzeug.pocoo.org/docs/0.11/datastructs/#werkzeug.datastructs.FileStorage http://werkzeug.pocoo.org/docs/0.11/datastructures/#werkzeug.datastructures.FileStorage

我需要使用烧瓶FileStorage测试框架和应用程序本身之外的实用程序的对象,本质上复制了使用表单上传文件的工作方式。这对我有用。

from werkzeug.datastructures import FileStorage
file = None
with open('document-test/test.pdf', 'rb') as fp:
    file = FileStorage(fp)
file.save('document-test/test_new.pdf')
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Python File 对象到 Flask 的 FileStorage 的相关文章

随机推荐