Flask send_file 未发送文件
Flask send_file not sending file
我将 Flask 与 send_file()
结合使用,让人们从服务器下载文件。
我目前的代码如下:
@app.route('/', methods=["GET", "POST"])
def index():
if request.method == "POST":
link = request.form.get('Link')
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
info_dict = ydl.extract_info(link, download=False)
video_url = info_dict.get("url", None)
video_id = info_dict.get("id", None)
video_title = info_dict.get('title', None)
ydl.download([link])
print("sending file...")
send_file("dl/"+video_title+".f137.mp4", as_attachment=True)
print("file sent, deleting...")
os.remove("dl/"+video_title+".f137.mp4")
print("done.")
return render_template("index.html", message="Success!")
else:
return render_template("index.html", message=message)
我添加 .f137.mp4
的唯一原因是因为我使用 AWS C9 作为我的在线 IDE 并且我无法安装 FFMPEG 来组合亚马逊上的音频和视频 Linux.但是,这不是问题。问题是它没有发送下载请求。
控制台输出如下:
127.0.0.1 - - [12/Dec/2018 16:17:41] "POST / HTTP/1.1" 200 -
[youtube] 2AYgi2wsdkE: Downloading webpage
[youtube] 2AYgi2wsdkE: Downloading video info webpage
[youtube] 2AYgi2wsdkE: Downloading webpage
[youtube] 2AYgi2wsdkE: Downloading video info webpage
WARNING: You have requested multiple formats but ffmpeg or avconv are not installed. The formats won't be merged.
[download] Destination: dl/Meme Awards v244.f137.mp4
[download] 100% of 73.82MiB in 00:02
[download] Destination: dl/Meme Awards v244.f140.m4a
[download] 100% of 11.63MiB in 00:00
sending file...
file sent, deleting...
done.
127.0.0.1 - - [12/Dec/2018 16:18:03] "POST / HTTP/1.1" 200 -
感谢您提供的所有帮助。谢谢!
您需要 return
send_file
的结果:
@app.route('/', methods=["GET", "POST"])
def index():
if request.method == "POST":
link = request.form.get('Link')
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
info_dict = ydl.extract_info(link, download=False)
video_url = info_dict.get("url", None)
video_id = info_dict.get("id", None)
video_title = info_dict.get('title', None)
ydl.download([link])
print("sending file...")
return send_file("dl/"+video_title+".f137.mp4", as_attachment=True)
else:
return render_template("index.html", message=message)
不幸的是,这会让您在发送文件后更难 "clean up",因此您可能希望将其作为定期维护的一部分(例如 运行 一个 cron 作业来删除旧文件下载的文件)。有关此问题的详细信息,请参阅 。
正如 Rob Bricheno 所说,
You need to return the result of send_file
所以你可以保存"flask.send_file"的结果,然后清理,然后return结果。
print("sending file...")
result = send_file("dl/"+video_title+".f137.mp4", as_attachment=True)
print("file sent, deleting...")
os.remove("dl/"+video_title+".f137.mp4")
return result
扩展@Rob Bricheno的回答,如果你需要在请求后清理,你可以创建一个延迟方法,在请求完成后执行:
@app.route('/', methods=["GET", "POST"])
def index():
if request.method == "POST":
link = request.form.get('Link')
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
info_dict = ydl.extract_info(link, download=False)
video_url = info_dict.get("url", None)
video_id = info_dict.get("id", None)
video_title = info_dict.get('title', None)
ydl.download([link])
print("sending file...")
response = send_file("dl/"+video_title+".f137.mp4", as_attachment=True)
# Create handle for processing display results (non-blocking, excecutes after response is returned)
@flask.after_this_request
def add_close_action(response):
@response.call_on_close
def process_after_request():
try:
print("file sent, deleting...")
os.remove("dl/"+video_title+".f137.mp4")
print("done.")
except Exception as e:
logger.exception(str(e))
return response
else:
return render_template("index.html", message=message)
我将 Flask 与 send_file()
结合使用,让人们从服务器下载文件。
我目前的代码如下:
@app.route('/', methods=["GET", "POST"])
def index():
if request.method == "POST":
link = request.form.get('Link')
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
info_dict = ydl.extract_info(link, download=False)
video_url = info_dict.get("url", None)
video_id = info_dict.get("id", None)
video_title = info_dict.get('title', None)
ydl.download([link])
print("sending file...")
send_file("dl/"+video_title+".f137.mp4", as_attachment=True)
print("file sent, deleting...")
os.remove("dl/"+video_title+".f137.mp4")
print("done.")
return render_template("index.html", message="Success!")
else:
return render_template("index.html", message=message)
我添加 .f137.mp4
的唯一原因是因为我使用 AWS C9 作为我的在线 IDE 并且我无法安装 FFMPEG 来组合亚马逊上的音频和视频 Linux.但是,这不是问题。问题是它没有发送下载请求。
控制台输出如下:
127.0.0.1 - - [12/Dec/2018 16:17:41] "POST / HTTP/1.1" 200 -
[youtube] 2AYgi2wsdkE: Downloading webpage
[youtube] 2AYgi2wsdkE: Downloading video info webpage
[youtube] 2AYgi2wsdkE: Downloading webpage
[youtube] 2AYgi2wsdkE: Downloading video info webpage
WARNING: You have requested multiple formats but ffmpeg or avconv are not installed. The formats won't be merged.
[download] Destination: dl/Meme Awards v244.f137.mp4
[download] 100% of 73.82MiB in 00:02
[download] Destination: dl/Meme Awards v244.f140.m4a
[download] 100% of 11.63MiB in 00:00
sending file...
file sent, deleting...
done.
127.0.0.1 - - [12/Dec/2018 16:18:03] "POST / HTTP/1.1" 200 -
感谢您提供的所有帮助。谢谢!
您需要 return
send_file
的结果:
@app.route('/', methods=["GET", "POST"])
def index():
if request.method == "POST":
link = request.form.get('Link')
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
info_dict = ydl.extract_info(link, download=False)
video_url = info_dict.get("url", None)
video_id = info_dict.get("id", None)
video_title = info_dict.get('title', None)
ydl.download([link])
print("sending file...")
return send_file("dl/"+video_title+".f137.mp4", as_attachment=True)
else:
return render_template("index.html", message=message)
不幸的是,这会让您在发送文件后更难 "clean up",因此您可能希望将其作为定期维护的一部分(例如 运行 一个 cron 作业来删除旧文件下载的文件)。有关此问题的详细信息,请参阅
正如 Rob Bricheno 所说,
You need to return the result of send_file
所以你可以保存"flask.send_file"的结果,然后清理,然后return结果。
print("sending file...")
result = send_file("dl/"+video_title+".f137.mp4", as_attachment=True)
print("file sent, deleting...")
os.remove("dl/"+video_title+".f137.mp4")
return result
扩展@Rob Bricheno的回答,如果你需要在请求后清理,你可以创建一个延迟方法,在请求完成后执行:
@app.route('/', methods=["GET", "POST"])
def index():
if request.method == "POST":
link = request.form.get('Link')
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
info_dict = ydl.extract_info(link, download=False)
video_url = info_dict.get("url", None)
video_id = info_dict.get("id", None)
video_title = info_dict.get('title', None)
ydl.download([link])
print("sending file...")
response = send_file("dl/"+video_title+".f137.mp4", as_attachment=True)
# Create handle for processing display results (non-blocking, excecutes after response is returned)
@flask.after_this_request
def add_close_action(response):
@response.call_on_close
def process_after_request():
try:
print("file sent, deleting...")
os.remove("dl/"+video_title+".f137.mp4")
print("done.")
except Exception as e:
logger.exception(str(e))
return response
else:
return render_template("index.html", message=message)