Flask的FileStorage的Python File对象 [英] Python File object to Flask's FileStorage
问题描述
我正在尝试在Flask中测试我的upload()方法.唯一的问题是Flask中的 FileStorage 对象具有python File 对象所没有的save()方法.
I'm trying to test my upload() method in Flask. The only problem is that the FileStorage object in Flask has a method save() which the python File object does not have.
我这样创建文件:
file = open('documents-test/test.pdf')
但是我无法测试我的upload()方法,因为该方法使用save().
But I cannot test my upload() method because that method uses save().
有什么想法将这个File对象转换为Flask Filestorage对象吗?
Any ideas how to convert this File object to a Flask Filestorage object?
推荐答案
http://werkzeug.pocoo.org/docs/0.11/datastructures/#werkzeug.datastructures.FileStorage
我需要将烧瓶 FileStorage
对象用于测试框架和应用程序本身之外的实用程序,从本质上复制了使用表单上载文件的方式.这对我有用.
I needed to use the flask FileStorage
object for a utility outside of the testing framework and the application itself, essentially replicating how uploading a file works using a form. This worked for me.
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')
这篇关于Flask的FileStorage的Python File对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!