Python 用Flask返回下载的文件

Python 用Flask返回下载的文件,python,flask,Python,Flask,我正在尝试使用flask程序返回下载的文件: # code to define title and get link try: YouTube(link).streams.first().download(filename=title) print("Successfully Downloaded") return send_from_directory('', filename=titl

我正在尝试使用flask程序返回下载的文件:

# code to define title and get link
        try:
            YouTube(link).streams.first().download(filename=title)
            print("Successfully Downloaded")
            return send_from_directory('', filename=title, as_attachment=True), "<h1>Complete</h1>"
        except Exception as e:
            exc_type, exc_obj, exc_tb = sys.exc_info()
            fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
            print(exc_type, fname, exc_tb.tb_lineno)
            return f"<h1>{e}</h1>"
有更好的方法吗?

您的
download()
函数将文件保存为
filename
+.mp4”,但您的
send\u from\u目录
只查找
filename
。 测试您的
返回时
仍然没有为我下载文件,因为
,“Complete”
。我把它取了下来,效果很好。 所以试试这个:

return send_from_directory('', filename=title+".mp4", as_attachment=True), "<h1>Complete</h1>"
return send_from_目录(“”,filename=title+“.mp4”,如_attachment=True),“Complete”
return send_from_directory('', filename=title+".mp4", as_attachment=True), "<h1>Complete</h1>"