Python Flask downloading a file returns 0 bytes

前端 未结 2 759
[愿得一人]
[愿得一人] 2021-01-12 23:50

Here is the code my flask server is running:

from flask import Flask, make_response
import os

app = Flask(__name__)

@app.route(\"/\")
def index():
                 


        
2条回答
  •  自闭症患者
    2021-01-13 00:17

    As danny wrote, you don't provide any content in your response, that's why you get 0 bytes. There is however an easy function send_file in Flask to return file content:

    from flask import send_file
    
    @app.route("/")
    def getFile(file_name):
        return send_file(file_name, as_attachment=True)
    

    Note that the file_name is relative to application root path (app.root_path) in this case.

提交回复
热议问题