简体   繁体   中英

Serving static files in Flask with Content-Length in HTTP response header

I am writing a small Python Flask application which allow people download file from my server. These files will be served by Python, not web server like Nginx or Apache.

I tried to use send_from_directory() and send_file() and I can download my file but there was no file-size because it was missed the Content-Length filed in the Header.

How can I added the header when I use send_from_directory() or send_file() . Or are there any other better way to do this.

Thank you.

If you use send_file() with a filename (not a file object), Flask will automatically set the Content-Length header for you.

send_from_directory() uses send_file() with a filename, so you are set there.

Do make sure you are using Flask 0.10 or newer; the header code was added in that version.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM