'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)
Solution 1:[1]
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)
Solution 2:[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)
Solution 3:[3]
return send_from_directory(directory=uploads, filename=filename)
change to
return send_from_directory(directory=uploads, path=filename, as_attachment=True)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | davidism |
| Solution 2 | Ganesan J |
| Solution 3 | Ihor Konovalenko |
