文件异步下载 / 带百分比
最后更新于:2022-04-02 03:19:03
[TOC]
## 文件异步下载
```js
下载
```
## 异步带下载百分比
异步下载文件需要在嵌套一层异步
```
//使用了 progressJs 插件
var fileDownDom = $("#filedown");
$.post(url,data,function(res){
var res.info=res.downurl
$.ajax({
type:'GET',
url: res.url,
xhrFields:{
onprogress:function(event){
var percent =(event.loaded/filesize)*100;
percent = percent.toFixed(2);
// 大于 1MB
var loaded=0,totalSize=0,unit='';
if (filesize>1000000){
loaded = event.loaded/1000000;
totalSize = filesize/1000000;
unit="MB";
// 小于 1MB
}else{
loaded = event.loaded/1000;
totalSize = filesize/1000;
unit="KB";
}
fileDownDom.find('.uploadify-progress-bar').css('width',percent+'%');
fileDownDom.find('.up_percent').text(percent);
fileDownDom.find('.uploadedsize').text(loaded.toFixed(2)+unit);
fileDownDom.find('.totalsize').text(totalSize.toFixed(2)+unit);
fileDownDom.find('.up_filename').text(res.info.file_name);
}
},
complete: function() {
fileDownDom.hide();
// 如果发现无法下载.添加 此行
location.href = res.url ;
},
});
},"JSON");
```
';