【问题标题】:Flask: send_from_directory and also refresh the page烧瓶:send_from_directory 并刷新页面
【发布时间】:2013-05-26 07:19:39
【问题描述】:

我在烧瓶中使用 send_from_directory,它允许用户下载某个文件。我想要实现的是下载文件后页面刷新,但我不确定如何使用烧瓶实现这一点(如果可能的话)。目前我的代码如下所示:

if report:
            location = Document_Generator(report).file_location

            return send_from_directory(location[0],
                               location[1], as_attachment=True)

所以我的问题是:如何刷新页面(返回带有模板的正常响应)以及允许用户下载文件?

【问题讨论】:

    标签: python python-2.7 flask


    【解决方案1】:

    HTTP 不允许你做你想做的事 (200 + 300)。您可以在客户端级别执行此操作,但使用 _target + JavaScript(或仅 JavaScript)。

    <a href="/path/to/download/file" target="downloadIframe">Download file</a>
    <iframe id="downloadIframe"></iframe>
    

    结合一些JavaScript:

    var slice = Array.prototype.slice;
    
    var links = document.querySelectorAll("[target='downloadIframe']"),
        iframe = document.getElementById("downloadIframe");
    slice.call(links).forEach(function(link) {
        link.addEventListener("click", reloadPageOnIframeLoad);
    });
    
    function reloadPageOnIframeLoad() {
        // Reset this for each click on a download link
        // rather than adding another event listener each time.
        iframe.onload = function() { window.location.reload(); };
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-21
      • 2021-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多