“另存为blob”需要很长时间才能从给定的url下载文件

Save as blob takes too long to download the file from given url

本文关键字:另存为 url 文件 下载 blob 长时间      更新时间:2023-09-26

我已经编写了以下服务来从给定的URL保存文件。

(function() {
    angular.module('SOME_APP')
        .service("downloadService", downloadService);
        function downloadService($http){
            var downloadFileFromUrl = downloadFileFromUrl;
            function downloadFileFromUrl(url){
                if(!url.length){
                    //TO-DO handle the error
                }
                else{
                    //find the file name and extension, to save it as:
                    var fileName;
                    for(var i=url.length; i>=0; i--){
                        if(url[i]=='/'){
                            fileName=url.slice(i+1, url.length);
                            console.log(fileName);
                            break;
                        }
                    }
                    $http({
                        url: url,
                        method: "GET",
                        responseType: 'arraybuffer'
                        }).success(function (data) {
                            var blob = new Blob([data], {type: '*/*'});
                            saveAs(blob, fileName);
                        }).error(function (data) {
                            console.log(data);
                            //TO-DO error handling
                    });
                }
            }
            return {
                    downloadFileFromUrl : downloadFileFromUrl
            }
        }
}());

当我调用该服务时,该服务首先下载文件,下载完成后,它会在浏览器中显示下载情况(进度为100%)。如何使其正常工作?(在浏览器中启动下载,并逐渐显示进度)

我的意图是从给定的url下载一个文件。我使用HTML5锚标记实现了它,下载属性为:

在index.html中:

               <a id='downloadTag' href="" download hidden></a>

在役:

                document.getElementById("downloadTag").href=url;
                document.getElementById("downloadTag").click();

这解决了我的问题。