-2

I am try download file from my flask application.

My method

@admin.route('/download_doc', methods=['GET', 'POST'])
def download_doc():
    doc_id = request.json['id']
    doc = Doc.query.filter(Doc.id==doc_id).first()
    filename = doc.filename
    uploads = os.path.join(app.root_path, app.config['UPLOAD_FOLDER'])
    return send_from_directory(directory=uploads, filename=filename)

Error 404. Why?

enter image description here

When I try print uploads variable. I see, not valid path.

enter image description here

Shato
  • 43
  • 6

1 Answers1

0

Paths with slashes and backslashes are ok for Windows. Check this question: Difference between forward slash (/) and backslash (\) in file path

However, what's weird on your screenshot is that you have two /app folders in your path and this can be an issue. Try to return files in this way:

return send_from_directory(app.config['UPLOAD_FOLDER'], filename, as_attachment=True)

Also, check documentation for send_from_directory method and hint about Sending files and Performance.

berserkk
  • 987
  • 6
  • 11
  • No, my app structure only one folder app. UPLOAD_FOLDER = 'app/static/doc', app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER. – Shato Jun 11 '17 at 11:32
  • Yes, but you're joining your `upload` folder with `root` folder, so you're getting two `/app` items in a result (as on your screenshot). – berserkk Jun 11 '17 at 11:35
  • not work do like this return send_from_directory(app.config['UPLOAD_FOLDER'], filename, as_attachment=True) – Shato Jun 11 '17 at 11:38
  • Your `UPLOAD_FOLDER` should be `'static/doc'`. – berserkk Jun 11 '17 at 12:03
  • After change show valid path, but not download file. Maybe permission problem? – Shato Jun 11 '17 at 12:34
  • It can be permission problem or file doesn't exist. Maybe you've missed file extension? – berserkk Jun 11 '17 at 14:52