【问题标题】:Download with <iframe> : works just one time使用 <iframe> 下载:只需一次
【发布时间】:2015-08-06 09:22:20
【问题描述】:

我有一个 web 服务向我发送一个 excel 文件,调用是通过 Ajax 进行的,逻辑如下:

  1. 发送“创建文件”请求。
  2. 询问进度,服务器会返回 0 到 100 之间的数字,客户端会用它更新进度条。
  3. 收到 100 个更新 iframe 的“src”元素后开始下载

到目前为止,使用 iframe 下载是使用 Internet Explorer 进行下载的唯一方法,所以我想保留它。

这些是我的 iframe 和进度条:

<iframe width="1" height="1" style="display:none" src="{{downloadLink}}"></iframe>

<progressbar class="progress" value="reportProgress" type="success" >
                {{reportProgressL}}
    </progressbar>

这是我的 Angular js 程序:

$scope.startDownload = function() {
    $scope.reportProgress = 0;
    $scope.reportProgressL = "0%";

    var promise = $http.get('/myServiceCall/createFile');
    promise.then(
            function(payload){
                console.log('success ');
                console.log(payload);
            },
            function(error){
                $scope.showAlert('danger',error);
            });

    $scope.askProgress();
};


$scope.askProgress = function(){
    $http.get('/myServiceCall/progress').success(function(response, status, headers, config){

        $scope.reportProgress = response.data;
        if(response.data < 100){
            $scope.reportProgressL = response.data+"%";
            $timeout(function() {$scope.askProgress();}, 1000); 
        }else{
            console.log('Server elaboration completed, download should start shortly...');
            $scope.reportProgressL = "Done!";
            $scope.downloadLink = '/myServiceCall/getFile'; //setting the src iframe attribute: this will start download
        }
    }).error(function(err, status, headers, config){
        alert('Error: '+err+" "+status);
    });
};

上面的代码运行良好……但只有一次!除非重新加载页面再次设置“src”属性不会做任何事情。为什么?

【问题讨论】:

    标签: javascript angularjs internet-explorer iframe


    【解决方案1】:

    问题是当您设置src 属性时,新值与旧值相同。浏览器不会检测到它已更改,也不会重新加载iframe。您应该尝试使用随机值设置?random=### 参数,以便属性发生变化。

    【讨论】:

    • 太棒了。很高兴我能帮上忙。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-05
    • 2023-03-26
    • 2011-04-12
    相关资源
    最近更新 更多