Flask: force download pdf files to open in browser Flask: force download pdf files to open in browser flask flask

Flask: force download pdf files to open in browser


You can try to add the mimetype parameter to send_from_directory:

return send_from_directory(directory=some_directory,                           filename=filename,                           mimetype='application/pdf')

That works for me, at least with Firefox.

If you need more control over headers, you can use a custom response, but you will lose the advantages of send_file() (I think it does something clever to serve the file directly from the webserver.)

with open(filepath) as f:    file_content = f.read()response = make_response(file_content, 200)response.headers['Content-type'] = 'application/pdf'response.headers['Content-disposition'] = ...return response