How to change downloading name in flask?

前端 未结 4 2103
名媛妹妹
名媛妹妹 2021-02-18 17:06

I have a small project which has to response some files. I know that using nginx will be better decision but that files a really small.

Part of my program:

相关标签:
4条回答
  • 2021-02-18 17:19

    I had this problem before, I tried many of solutions mentioned here and finally I could fix the problem by writing the correct mimetype of the file

    here my example:

    @app.route('/download_report')
    def download_summary():
        return send_file('Report.csv', mimetype='application/x-csv', attachment_filename='summary_report.csv', as_attachment=True)
    
    0 讨论(0)
  • 2021-02-18 17:20

    You need to set a Content-Disposition: attachment; filename=.... HTTP header for the browser to use the correct filename.

    You can have send_file() set this header for you by setting the as_attachment=True argument. The filename is then taken from the file object you passed in. Use the attachment_filename argument to explicitly set a different filename:

    return send_file(os.path.join(filepath, filename), as_attachment=True)
    

    From the flask.send_file documentation:

    • as_attachment – set to True if you want to send this file with a Content-Disposition: attachment header.
    • attachment_filename – the filename for the attachment if it differs from the file’s filename.

    You may want to use the flask.send_from_directory() function instead. That function first ensures that the filename exists (raising a NotFound if not), and ensures that the filename doesn't contain any .. relative elements that might be used to 'escape' the directory. Use this for all filenames taken from untrusted sources:

    return send_from_directory(filepath, filename, as_attachment=True)
    
    0 讨论(0)
  • 2021-02-18 17:30

    In some cases the filename is still not visible. To have it for sure you should set "x-filename" header and to expose this header.

    from flask import send_file
    response = send_file(absolute_image_path, mimetype='image/jpeg', attachment_filename=name, as_attachment=True)
    response.headers["x-filename"] = name
    response.headers["Access-Control-Expose-Headers"] = 'x-filename'
    return response
    
    0 讨论(0)
  • In my case, setting the as_attachment and attachment_filename did not work because the Content-Disposition: attachment does not appear in the client-side copy of the response.

    If you have Flask-CORS enabled, you can initialize it with expose_headers set to a custom header that specifies the filename (ex. "x-suggested-filename"). Then add that header to the response.

    # In file where the Flask app instance is initialized
    app = Flask(__name__)
    CORS(app, expose_headers=["x-suggested-filename"])
    
    # In file with the download endpoint
    result = send_file("/full/path/to/some/file",
                       mimetype="text/plain", # use appropriate type based on file
                       as_attachment=True,
                       conditional=False)
    result.headers["x-suggested-filename"] = "use_this_filename.txt"
    return result
    

    Then, in the client-side download code, you can inspect the response headers to get the filename from the same custom header:

    # Using axios and FileSaver
    let response = await axios.get(downloadUrl, downloadConfig);
    let filename = response.headers["x-suggested-filename"];
    FileSaver.saveAs(response.data, filename);
    
    0 讨论(0)
提交回复
热议问题