gpt4 book ai didi

python - Flask send_file 不发送文件

转载 作者:太空宇宙 更新时间:2023-11-04 00:07:10 28 4
gpt4 key购买 nike

我正在使用带有 send_file() 的 Flask 让人们从服务器下载文件。

我目前的代码如下:

@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 以在 Amazon 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 -

感谢任何帮助。谢谢!

最佳答案

您需要返回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)

不幸的是,这会使您在发送文件后更难“清理”,因此您可能希望将其作为定期维护的一部分(例如,运行 cron 作业以删除旧的下载文件)。参见 here有关该问题的更多信息。

关于python - Flask send_file 不发送文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53747258/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com