K
K
kolomiec_artiom2020-02-24 02:43:22
Flask
kolomiec_artiom, 2020-02-24 02:43:22

How to fix error when using send_file in Flask?

Goodnight! I'm trying to transfer a file to a user not from my server, acting as an "intermediary"

# Download from provided URL.
@app.route('/<path:url>')
def download(url):
    req = requests.get(url, stream=True)
    return send_file(stream_with_context(req.iter_content(1024)), mimetype='mp3')


When a user follows the link mysite.ru/anothersite.mp3, he should start downloading a file from someone else's server, but I get an error:
AttributeError: 'generator' object has no attribute 'read'
. How can it be corrected?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2020-02-24
@kolomiec_artiom

You can try like this

response = Response(stream_with_context(req.iter_content()), mimetype='audio/mpeg')
response.headers['Content-Disposition'] = 'attachment; filename=some_file_name.mp3'
return response

But it is possible that the upstream will still have to be completely pulled out first, and then given to the client.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question