eightHundreds
1/17/2018 - 2:19 AM

通过ajax下载文件

function downloadFile(urlToSend) {//JQ做不到这样
     var req = new XMLHttpRequest();
     req.open("GET", urlToSend, true);
     // req.setRequestHeader('Content-Type', 'application/json;charset=UTF-8');
     req.responseType = "blob";
     req.onload = function (event) {
         var blob = req.response;
         //文件名很难办,https://stackoverflow.com/questions/40939380/how-to-get-file-name-from-content-disposition
         var fileName = req.getResponseHeader("fileName") //if you have the fileName header available
         var link=document.createElement('a');
         link.href=window.URL.createObjectURL(blob);
         link.download=fileName;
         link.click();
     };

     req.send();
     //req.send(JSON.stringify({tags: tags}));
 }