Send_file () when calling a returned text document instead of an image

I want to send an image file from the server to the client. I use flask structure.

But the problem is that whenever I call the route in which send_file(), the response returns a file. When I click this file, it geditopens it with nothing in this file. This means that it must be a text file.

I called flag documents for send_file().

Here is what I do in the code:

@app.route('/try')
def trial():
    todown = 'https://igcdn-photos-e-a.akamaihd.net//hphotos-ak-xaf1//t51.2885-15//e35//12093691_1082288621781484_1524190206_n.jpg'  
    resp = requests.get(todown)
    return send_file(resp,mimetype="image/jpeg",attachment_filename="img.jpg",as_attachment=True)

Whenever I upload localhost:5000/try, the file is uploaded, but not the image file I want to upload.

The error I get in my terminal is AttributeError: 'Response' object has no attribute 'read' error.

What is the problem. Is there something missing in this fragment above?

+4
1
  • resp - requests.models.Response, :

    >>> import requests
    >>> todown = 'https://igcdn-photos-e-a.akamaihd.net//hphotos-ak-xaf1//t51.2885-15//e35//12093691_1082288621781484_1524190206_n.jpg'
    >>> resp = requests.get(todown)
    >>> resp
    <Response [200]>
    >>> type(resp)
    <class 'requests.models.Response'>
    
  • Flask.send_file() .


, resp.content , (, , resp.text . .content, , ).

>>> import requests
>>> todown = 'https://igcdn-photos-e-a.akamaihd.net//hphotos-ak-xaf1//t51.2885-15//e35//12093691_1082288621781484_1524190206_n.jpg'
>>> resp = requests.get(todown)
>>> type(resp.content)
<class 'bytes'>

.


, Flask.send_file() , .

, io.BytesIO , . : io.StringIO, .

:

import requests
from io import BytesIO
from flask import Flask, send_file

app = Flask(__name__)

@app.route('/')
def tria():
    todown = 'https://igcdn-photos-e-a.akamaihd.net//hphotos-ak-xaf1//t51.2885-15//e35//12093691_1082288621781484_1524190206_n.jpg'
    resp = requests.get(todown)
    return send_file(BytesIO(resp.content), mimetype="image/jpeg", attachment_filename="img2.jpg", as_attachment=True)

app.run(port=80, debug=True)

, , , . tempfile.NamedTemporaryFile(), , , .

:

, TemporaryFile(), , ( Unix ).

name . , , ( Unix, Windows NT ). delete ( ), , .

, file . with, .

:

import tempfile
import requests
from flask import Flask, send_file

app = Flask(__name__)


@app.route('/')
def tria():
    todown = 'https://igcdn-photos-e-a.akamaihd.net//hphotos-ak-xaf1//t51.2885-15//e35//12093691_1082288621781484_1524190206_n.jpg'

    resp = requests.get(todown)

    with tempfile.NamedTemporaryFile() as f:  
        # create a file-like object use `NamedTemporaryFile()` and `with` 
        # as the basic usage which said in the document     

        f.write(resp.content)
        # write the content of the image into it

        return send_file(f.name, mimetype="image/jpeg",
                         attachment_filename="img2.jpg", as_attachment=True)                             
        # `f.name` is the temp file filename

app.run(port=80, debug=True)
+10

Source: https://habr.com/ru/post/1616668/


All Articles