Python File object to Flask's FileStorage

flask, python, testing

Solution

http://werkzeug.pocoo.org/docs/0.11/datastructures/#werkzeug.datastructures.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')

Problem

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. I create my file like this: ``` file = open('documents-test/test.pdf') ``` But I cannot test my upload() method because that method uses save(). Any ideas how to convert this File object to a Flask Filestorage object?

Original source