leo_dicaprio_of_data
leo_dicaprio_of_data

Reputation: 91

Flask shows TypeError: send_from_directory() missing 1 required positional argument: 'path'

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)

Upvotes: 9

Views: 14598

Answers (4)

Arunisto
Arunisto

Reputation: 13

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)

Upvotes: 0

Ganesan J
Ganesan J

Reputation: 646

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)

Upvotes: 2

arunraj
arunraj

Reputation: 49

return send_from_directory(directory=uploads, filename=filename)

change to

return send_from_directory(directory=uploads, path=filename, as_attachment=True)

Upvotes: 3

Josh Liu
Josh Liu

Reputation: 476

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)

Upvotes: 19

Related Questions