9

When I deploy my Flask app on Azure, the view raises TypeError: send_from_directory() missing 1 required positional argument: 'path'. This isn't happening when I run locally.

from flask import send_from_directory

@app.route('/download/<path:filename>', methods=['GET', 'POST'])
def download(filename):
    uploads = os.path.join(app.root_path, app.config['UPLOAD_FOLDER'])
    return send_from_directory(directory=uploads, filename=filename)

4 Answers 4

19

Change the final line to return send_from_directory(uploads, filename).

See the Flask docs about send_from_directory. The changelog at the bottom that says "Changed in version 2.0: path replaces the filename parameter."

If you still want to use named parameters, change filename= to path=. send_from_directory(directory=uploads, path=filename)

0
3
return send_from_directory(directory=uploads, filename=filename)

change to

return send_from_directory(directory=uploads, path=filename, as_attachment=True)
1
  • 1
    While this code snippet may solve the problem, it doesn't explain why or how it answers the question. Please include an explanation for your code, as that really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Commented Jan 11, 2022 at 7:34
2

In my case I need certificate, it is saved inside static/pdf/certficates folder

@app.route('/download/<filename>', methods = ["GET", "POST"])
def download(filename):
   uploads = os.path.join(current_app.root_path, "static/pdf/folder_name")
   return send_from_directory(directory=uploads,path=filename,as_attachment=True)
0

Use send_file instead of send_from_directory

from flask import send_file


@app.route("/download")
def download():
     return send_file('<file_path>', as_attachment=True)

Always remember if your file is on your current directory where your app.py then don't give the complete path just enter the filename. But if your file is in a folder where your app.py file was then give the name with the folder name like below,

@district_admin_bp.route("/download_pc")
def download_pc():
    filename = "pc.xlsx"
    return send_file("static\\report_upload\\pc_master\\"+filename, as_attachment=True)

Not the answer you're looking for? Browse other questions tagged or ask your own question.