【问题标题】:flask html link for downloading file用于下载文件的烧瓶 html 链接
【发布时间】:2018-05-09 14:02:09
【问题描述】:

我正在构建一个允许用户从我的服务器下载文件的网站。 我写了一个提供文件的路由:

@api.route('/download_results/<path:filename>')
def download_results(filename):
    return send_from_directory(app.config['UPLOAD_FOLDER'], filename, as_attachment=True)

如果我尝试使用这样的 html 标签:

<a class="btn btn-primary" herf="http://localhost/api/download_results/1234567890/results.zip" download="results.zip">Download results</a>

它不起作用。但是如果我在 jQuery 中添加一个事件监听器:

$('a').on('click', function(event) {
     event.preventDefault();
     window.location.href = location.origin + '/api/download_results/' + data.parsedData[0];
 });

它有效,我可以下载,但我收到警告:

资源被解释为 Document 但以 MIME 类型传输 应用程序/x-zip 压缩: “http://localhost/api/download_results/1234567890/results.zip”。

任何人都知道我做错了什么以及如何解决这个问题? 谢谢!

【问题讨论】:

  • 可能是地址中缺少端口?
  • 我尝试添加端口,但我得到了相同的行为......

标签: javascript python jquery html flask


【解决方案1】:

您的锚标记的 href 属性有错别字。

【讨论】:

    猜你喜欢
    • 2014-08-25
    • 2021-07-15
    • 2021-06-23
    • 2020-04-05
    • 2021-08-28
    • 1970-01-01
    • 2016-01-31
    • 1970-01-01
    • 2021-09-17
    相关资源
    最近更新 更多