0

I have a question about flask werkzeug.filestorage

I wrote the code using flask but I got a question

I use the code to get the image file data

request.files['file'] is the werkzeug.datastructures.FileStorageobject

request.files['file'].stream works well.

Read file data without saving it in Flask this question says, use like request.files['file'].read() but it occur error

I want to know why it occur errors and the difference between above code and the example of the use.

This is my total code

from PIL import Image
app.route('/get_image', methods = ['POST'])
def image_check():
    # i works well
    img = Image.open(request.files['file'].stream)

    # it occur error
    # img = Image.open(request.files['file'].read())

    logger.info("file {}".format(type(request.files['file'])))
furas
  • 134,197
  • 12
  • 106
  • 148
  • 1
    `Image.open()` expects either a filename (which will be opened for you), or an opened file or similar object (which will have `.read()` called on it). It is not prepared to receive the contents of the file directly. – jasonharper Jul 26 '21 at 03:25
  • question from your link doesn't use `Image.open()` and this make big difference. `request.files['file'].read()` will work with functions which expect data - like standard `write(data)` – furas Jul 26 '21 at 05:03

0 Answers0