【问题标题】:browser doesn't show progress bar when downloading a file using javascript使用javascript下载文件时浏览器不显示进度条
【发布时间】:2022-11-11 23:53:31
【问题描述】:

下载文件时浏览器不显示进度条

function getSound(sound) {
    var req = new XMLHttpRequest();
    req.open("GET", sound, true);
    req.responseType = "blob";
    req.onload = function (event) {
        var blob = req.response;//if you have the fileName header available
        var link=document.createElement('a');
        link.href=window.URL.createObjectURL(blob);
        link.download='sound.mp3';
        link.click();
    };
    req.send();
}

我想要这样的节目

【问题讨论】:

    标签: javascript


    【解决方案1】:

    问题是,当您调用“单击”时,文件已经下载。但是您不需要提前下载文件:

    function getSound(sound) {
      const link=document.createElement('a');
      link.href=sound;
      link.download='sound.mp3';
      link.click();
    }
    

    【讨论】:

    • 从这个方法,它导航到另一个页面。但我需要在同一页面内下载
    • @Lakmi 是与您的网站不同域上的链接吗?如果它在同一个域上,它应该可以工作。
    【解决方案2】:

    您必须添加 Content-Lenght 标头以让 Web 浏览器知道您的文件的大小。

    【讨论】:

      【解决方案3】:

      @Lakmi,你得到这个问题的解决方案了吗?我需要类似的东西来实现

      【讨论】:

        猜你喜欢
        • 2019-08-26
        • 2014-11-18
        • 2017-02-15
        • 2016-07-30
        • 1970-01-01
        • 2014-05-03
        • 1970-01-01
        • 2010-10-18
        • 1970-01-01
        相关资源
        最近更新 更多