vue,React中利用的axios进行下载word,excel,pdf 常见的问题
/* 下载方法 */ function downFile(blob, fileName) { if (window.navigator.msSaveOrOpenBlob) { navigator.msSaveBlob(blob, fileName); } else { var link = document.createElement('a'); link.href = window.URL.createObjectURL(blob); link.download = fileName; link.click(); window.URL.revokeObjectURL(link.href); } } /* 下载word */ Axios(api, params, {responseType: 'arrya'}) .then((res) => { let blob = new Blob([res], {type: "application/msword"}); let fileName = `${row.task_name}.docx` downFile(blob, fileName) }); /* 下载文本 */ let blob = new Blob(['文件内容:hello world !'], {type: "application/octet-binary"}); let fileName = `${row.task_name}.txt`; downFile(blob, fileName)
方法二:
// Axios(api, params, {responseType: 'blob'}) // .then((res) => { // const content = res // const blob = new Blob([content]) // const fileName = '测试表格.docx' // if ('download' in document.createElement('a')) { // 非IE下载 // const elink = document.createElement('a') // elink.download = fileName // elink.style.display = 'none' // elink.href = URL.createObjectURL(blob) // document.body.appendChild(elink) // elink.click() // URL.revokeObjectURL(elink.href) // 释放URL 对象 // document.body.removeChild(elink) // } else { // IE10+下载 // navigator.msSaveBlob(blob, fileName) // } // });
方法三:
// Axios(api, params, {responseType: 'blob'}) // .then((res) => { // FileDownload(res, 'word.docx'); // let blob = new Blob([res], {type: "application/octet-stream"}); // let objectUrl = URL.createObjectURL(blob); // window.location.href = objectUrl; // });
vue,React中利用的axios进行下载word,excel,pdf 常见的问题
/* 下载方法 */ function downFile(blob, fileName) { if (window.navigator.msSaveOrOpenBlob) { navigator.msSaveBlob(blob, fileName); } else { var link = document.createElement('a'); link.href = window.URL.createObjectURL(blob); link.download = fileName; link.click(); window.URL.revokeObjectURL(link.href); } } /* 下载word */ Axios(api, params, {responseType: 'arrya'}) .then((res) => { let blob = new Blob([res], {type: "application/msword"}); let fileName = `${row.task_name}.docx` downFile(blob, fileName) }); /* 下载文本 */ let blob = new Blob(['文件内容:hello world !'], {type: "application/octet-binary"}); let fileName = `${row.task_name}.txt`; downFile(blob, fileName)
方法二:
// Axios(api, params, {responseType: 'blob'}) // .then((res) => { // const content = res // const blob = new Blob([content]) // const fileName = '测试表格.docx' // if ('download' in document.createElement('a')) { // 非IE下载 // const elink = document.createElement('a') // elink.download = fileName // elink.style.display = 'none' // elink.href = URL.createObjectURL(blob) // document.body.appendChild(elink) // elink.click() // URL.revokeObjectURL(elink.href) // 释放URL 对象 // document.body.removeChild(elink) // } else { // IE10+下载 // navigator.msSaveBlob(blob, fileName) // } // });
方法三:
// Axios(api, params, {responseType: 'blob'}) // .then((res) => { // FileDownload(res, 'word.docx'); // let blob = new Blob([res], {type: "application/octet-stream"}); // let objectUrl = URL.createObjectURL(blob); // window.location.href = objectUrl; // });