最近在使用Flask框架做一個小專案的時候遇到一個問題,使用Flask的返回檔
- return send_from_directory(
- directory=app.config["CLIENT_IMAGES"], filename=image_name, as_attachment=False
- )
複製代碼
錯誤訊息
TypeError: send_from_directory() missing 1 required positional argument: 'path'
改成
- @app.route("/get-image/<path:image_name>")
- def get_image(image_name):
- print(app.config["CLIENT_IMAGES"])
- try:
- return send_from_directory(
- directory=app.config["CLIENT_IMAGES"], path=image_name, as_attachment=False
- )
- except FileNotFoundError:
- abort(404)
複製代碼
正常秀出
Flask send_from_directory path 參數
改參數,就變成直接下載
as_attachment=True
Flask send_from_directory path 參數
|