6

Here is the code my flask server is running:

from flask import Flask, make_response
import os

app = Flask(__name__)

@app.route("/")
def index():
        return str(os.listdir("."))

@app.route("/<file_name>")
def getFile(file_name):
        response = make_response()
        response.headers["Content-Disposition"] = ""\
        "attachment; filename=%s" % file_name
        return response    

if __name__ == "__main__":
        app.debug = True
        app.run("0.0.0.0", port = 6969)

If the user goes to the site, it prints the files in the directory. However if you go to site:6969/filename it should download the file. However I am doing something wrong as the file size always 0 bytes and the downloaded file has no data in it. Any thoughts. I tried adding the content-length header and that didn't work. Don't know what else it could be.

Steven K
  • 441
  • 6
  • 14

2 Answers2

10

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("/<file_name>")
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.

Bulat
  • 262
  • 5
  • 8
  • Yep sounds good. Or as Martijn Pieters mentioned in the above comment thread, [send_from_directory()](https://flask.readthedocs.org/en/latest/api/#flask.send_from_directory) works as well. Thanks! – Steven K Aug 24 '14 at 22:29
7

All that header does is tell the browser to treat the response data as a downloadable file with a certain name. It doesn't actually set any response data which is why it's blank.

You'd need to set the file contents on the response for it to work.

@app.route("/<file_name>")
def getFile(file_name):
    headers = {"Content-Disposition": "attachment; filename=%s" % file_name}
    with open(file_name, 'r') as f:
        body = f.read()
    return make_response((body, headers))

EDIT - Cleaned up code a little based on api docs

danny
  • 10,103
  • 10
  • 50
  • 57
  • That makes sense. Then just a follow up question. There would be no difference then between your code above doing something like this instead right?: with open(file_name, 'r') as f: response = make_response(f.read()) response.headers["Content-Disposition"] = ""\ "attachment; filename=%s" % file_name return response. Thanks Danny! – Steven K Apr 29 '14 at 13:13
  • Yeah that should work, you can also pass headers directly to `make_response` (I edited the answer, should work with Flask 0.9+). – danny Apr 29 '14 at 14:11
  • Last question Danny. Where did you find that you response object has a content method. Before your edit you had "response.content("Content goes here.")" I looked here http://flask.pocoo.org/docs/api/#flask.Flask.response_class but could not find any documentation on it. – Steven K Apr 29 '14 at 15:59
  • Never mind I was trying your old solution with response.content = "foo" and that was not working so I assume it is deprecated. Your new solution shown in your example and in the api docs works. Thanks! – Steven K Apr 29 '14 at 18:27
  • 3
    Better still, use [`send_from_directory()`](https://flask.readthedocs.org/en/latest/api/#flask.send_from_directory). – Martijn Pieters Apr 30 '14 at 12:20