js 文件下载 进度条

2021-05-17 10:28

阅读:460

标签:log   document   style   send   文件下载   define   navig   new   ica   

 

js:

 /**
  * 下载文件 - 带进度监控
  * @param url: 文件请求路径
  * @param name: 保存的文件名
  * @param progress: 进度处理回调函数
* eg: progressDownLoad({url:‘http://loacalhost:8080/downLoad.action‘,‘file.rar‘,progress:function(percent){
* console.log(percent);
* }}); *
*/ function progressDownLoad({url,filename,progress}){ var req = new XMLHttpRequest(); req.open("POST", url, true); //监听进度事件 req.addEventListener("progress", function (evt) { if (evt.lengthComputable) { var percentComplete = evt.loaded / evt.total; if(progress) try{ progress.call(percentComplete); }catch(e){} } }, false); req.responseType = "blob"; req.onreadystatechange = function () { if (req.readyState === 4 && req.status === 200) { if (typeof window.chrome !== ‘undefined‘) { // Chrome version var link = document.createElement(‘a‘); link.href = window.URL.createObjectURL(req.response); link.download = filename; link.click(); } else if (typeof window.navigator.msSaveBlob !== ‘undefined‘) { // IE version var blob = new Blob([req.response], { type: ‘application/force-download‘ }); window.navigator.msSaveBlob(blob, filename); } else { // Firefox version var file = new File([req.response], filename, { type: ‘application/force-download‘ }); window.open(URL.createObjectURL(file)); } } }; req.send(); }

 

js 文件下载 进度条

标签:log   document   style   send   文件下载   define   navig   new   ica   

原文地址:http://www.cnblogs.com/xtreme/p/7744065.html


评论


亲,登录后才可以留言!