How to save image which sent via flask send_file

Issue

I have this code for server

@app.route('/get', methods=['GET'])
def get():
    return send_file("token.jpg", attachment_filename=("token.jpg"), mimetype='image/jpg')

and this code for getting response

r = requests.get(url + '/get')

And i need to save file from response to hard drive. But i cant use r.files. What i need to do in these situation?

Solution

Assuming the get request is valid. You can use use Python’s built in function open, to open a file in binary mode and write the returned content to disk. Example below.

file_content = requests.get('http://yoururl/get')
save_file = open("sample_image.png", "wb")
save_file.write(file_content.content)
save_file.close()

As you can see, to write the image to disk, we use open, and write the returned content to ‘sample_image.png’. Since your server-side code seems to be returning only one file, the example above should work for you.

Answered By – basic197

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published