EXCEL文件下载(js、java)

/** * @param target_URL 下载地址 * @param onload 服务器返回结果的回调函数 * @param fileName 文件名 不传则从服务端获取 Content-disposition filename=* * @param save_URL 保存路径 先不写了 */ function downLoadFile(target_url,onloadFunction,filename,save_URL){ var req = new XMLHttpRequest(); req.open("POST", target_url, true); req.responseType = "blob"; req.onreadystatechange = function () { if (req.readyState === 4 && req.status === 200) { if(!filename){ filename = req.getResponseHeader("Content-disposition"); filename = filename.substring(filename.indexOf("filename=")+9,filename.length); filename = decodeURI(filename); } 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.onload = onloadFunction; req.ontimeout = function(e) { //下载超时 }; req.onerror = function(e) { //下载出错 }; req.send(); }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zwjygf.html