简体   繁体   English

flask send_from_directory 内的文件表示

[英]flask send_from_directory within a file representation

Hi I currently have a flask app that shows files and their details, now I want flask to also allow file downloads from the page of the represented file嗨,我目前有一个 flask 应用程序,它显示文件及其详细信息,现在我希望 flask 也允许从代表文件的页面下载文件

I tried a couple things including send_from_directory() which didn't work.我尝试了几件事,包括 send_from_directory() 没有用。 So my question is how can I add a working download link to the page?所以我的问题是如何向页面添加有效的下载链接?

@app.route('/browser/<path:urlFilePath>')
def browser(urlFilePath):
    nestedFilePath = os.path.join(FILE_SYSTEM_ROOT, urlFilePath)
    if os.path.isdir(nestedFilePath):
        itemList = os.listdir(nestedFilePath)
        fileProperties = {"filepath": nestedFilePath}
        if not urlFilePath.startswith("/"):
            urlFilePath = "/" + urlFilePath
        return render_template('browse.html', urlFilePath=urlFilePath, itemList=itemList)
    if os.path.isfile(nestedFilePath):
        fileProperties = {"filepath": nestedFilePath}
        sbuf = os.fstat(os.open(nestedFilePath, os.O_RDONLY)) #Opening the file and getting metadata
        fileProperties['type'] = stat.S_IFMT(sbuf.st_mode) 
        fileProperties['mode'] = stat.S_IMODE(sbuf.st_mode) 
        fileProperties['mtime'] = sbuf.st_mtime 
        fileProperties['size'] = sbuf.st_size 
        if not urlFilePath.startswith("/"):
            urlFilePath = "/" + urlFilePath
        return render_template('file.html', currentFile=nestedFilePath, fileProperties=fileProperties)
    return 'something bad happened'

@app.route('/downloads/<path:filename>', methods=['GET', 'POST'])
def download(filename):
    uploads = os.path.join(current_app.root_path, app.config['UPLOAD_FOLDER'])
    return send_from_directory(directory=uploads, filename=filename)

And with the following HTML并与以下 HTML

{% extends 'base.html' %}


{% block header %}
<h1>{% block title %}Filebrowser{% endblock %}</h1>
{% endblock %}
{% block content %}
    <p>Current file: {{ currentFile }}</p>
    <p>
        <table>
            {% for key, value in fileProperties.items() %}
            <tr>
                <td>{{ key }}</td>
                <td>{{ value }}</td>
            </tr>
            <tr>
                <a href="{{ url_for('downloads', ['image_name']) }}">File</a>
            </tr>

            {% endfor %}


        </table>
    </p>
    {% endblock %}

I want the download link on that page to work for the {{ currentFile }} can anyone help me out?我希望该页面上的下载链接适用于 {{ currentFile }} 任何人都可以帮助我吗?

Firstly, the link to the file in your file.html template should probably go outside the for loop -- otherwise, the link will appear multiple times which is probably not what you want.首先,文件中文件的链接file.html模板可能应该在for循环之外go ——否则,链接将出现多次,这可能不是你想要的。

Secondly, based on your /downloads route, your call to url_for for the download link doesn't match up.其次,根据您的/downloads路线,您对url_for的下载链接调用不匹配。 It should be:它应该是:

<a href="{{ url_for('download', filename=currentFile) }}">File</a>

You need to supply the filename as an argument so that the flask server can match it to the route & in url_for , you need to supply the name of the function - which in this case is download instead of downloads .您需要提供filename作为参数,以便 flask 服务器可以将其与路由匹配,并且在url_for中,您需要提供function的名称 - 在这种情况下是download而不是downloads

Lastly, your /browser route prepends the subdirectory to the filename - so when you pass currentFile to the HTML template, it will contain the directory prefix -- which you will want to strip, otherwise your link wouldn't work.最后,您的/browser路由将子目录添加到文件名 - 因此,当您将currentFile传递给 HTML 模板时,它将包含目录前缀 - 您将要删除它,否则您的链接将不起作用。 The file download would then work since in your /downloads route, you prefix the filename with the directory anyway.然后文件下载将起作用,因为在您的/downloads路由中,无论如何您都要在filename加上目录。 Hence, when you render the HTML template, use os.path.basename() to obtain the filename without the directory, ie因此,当您渲染 HTML 模板时,使用os.path.basename()获取没有目录的文件名,即

return render_template('file.html', currentFile=os.path.basename(nestedFilePath), fileProperties=fileProperties)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM