名称错误:全局名称'send_from_directory'未定义

2024-05-21 19:44:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用Flask,并试图提供一个文件供用户下载。

我的代码如下:

@app.route('/downloads/<string:yt_id>')
def download_file(yt_id):

    def hooks(data):
        if data['status'] == 'finished':
            filename = data['filename']

    ydl_opts = {
        'format': 'bestaudio/best',
        'postprocessors': [{
            'key': 'FFmpegExtractAudio',
            'preferredcodec': 'mp3',
            'preferredquality': '192',
        }],
        'progress_hooks': [hooks],
    }
    with youtube_dl.YoutubeDL(ydl_opts) as ydl:
        ydl.download(['http://www.youtube.com/watch?v='+yt_id])


    return send_from_directory(".",
                               filename,
                               as_attachment=True)

我想以上代码中唯一相关的部分是:

    return send_from_directory(".",
                               filename,
                               as_attachment=True)

这是我收到的错误信息:

NameError: global name 'send_from_directory' is not defined

我已经看了几个关于人们如何使用send_from_directory的例子,我看不出我所做的有多大区别。因此,任何帮助都将不胜感激。


Tags: 代码fromsendiddatayoutubedownloaddef