Issue with send_file() and send_from_directory() not sending #5894
-
|
I'm following the pattern here to try and send a file from temporary storage and I'm encountering an error that I don't know how to debug. I was hoping somebody here can help. I have a view function that redirects to a function that calls send_file() (I've tried both send_file() and send_from_directory). I'm not seeing any exceptions and everything seems to work fine, but the file is not sending. When I open up dev tools in Chrome or Edge, however i can see the file in the network panel (no error code there either -- it redirected with status code 200). And if I double-click it it will then be downloaded as an attachment. Any clue why it is not sending automatically? Not sure if it matters, but I'm using Flask==2.3.2, but I've tried with Flask==3.1.2 as well. Here's the gist of what I'm doing: @app.route('/some-function', methods=['GET','POST'])
def some_function():
# some stuff happens (I define filename and do some other simple things)
if request.method == 'POST':
updates = request.data # get some data -- this works fine
df = pandas_function(updates) # do some stuff with that data -- this works fine
df.to_excel(os.path.join(app.cofing['UPLOAD_FOLDER'], filename) # write file -- this works fine
return redirect(url_for('download_file', filename=filename))
return render_template('some_template.html')
@app.route('/uploads/<filename>')
def download_file(filename):
print('i just redirected') # this prints so it seems like it redirected successfully
return send_from_directory(os.path.join(current_app.root_path, app.config['UPLOAD_FOLDER']), filename, as_attachment=True)Thanks for any help you can offer! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
|
You're returning text (a URL), not a redirect. Use |
Beta Was this translation helpful? Give feedback.
There is nothing in Flask that would cause a redirect to prevent the download of a file being served. There is no way for the server to stop the browser from downloading a file it sends. That sounds like your browser or your frontend.