AJAX 根据URL多文件下载

 DownLoad() {
     const that = this;
     const result = "http://" + window.location.host + "/Upload/"
     let files = that.DownLoadUrl.split(";") //分号分割的url地址
     files.forEach(url => {
         let dowmUrl = result + url;
         that.getFileAndDownload(dowmUrl);
     })
 },
 getFileAndDownload(url) {
     let x = new XMLHttpRequest();
     x.open('GET', url, true)
     x.responseType = 'blob'
     x.onload = function (e) {
         const fileName = url.substring(url.lastIndexOf("/") + 1, url.length);
         const blob = x.response
         if ('msSaveOrOpenBlob' in navigator) {
             window.navigator.msSaveOrOpenBlob(blob, fileName)
         } else {
             var a = document.createElement('a')
             a.download = fileName;
             a.href = URL.createObjectURL(blob);
             $('body').append(a);
             a.click();
             $(a).remove();
         }
     }
     x.send();
 },

 

posted @ 2022-10-19 11:06  微风吹过~  阅读(67)  评论(0编辑  收藏  举报