js post请求下载文件
生活随笔
收集整理的這篇文章主要介紹了
js post请求下载文件
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
js 下載post請(qǐng)求的文件
/* content 文件二進(jìn)制內(nèi)容 fileName 文件名 */ downloadFile(content, fileName) {(fileName && fileName.indexOf('.') !== -1) && (fileName = fileName.slice(0, fileName.indexOf('.')));const blob = new Blob([content], {type: content.type})if ('download' in document.createElement('a') && navigator.userAgent.indexOf('Edge') <= -1) { // 非IE 及edge下載const elink = document.createElement('a')fileName && (elink.download = fileName)elink.style.display = 'none'elink.href = URL.createObjectURL(blob)document.body.appendChild(elink)elink.click()URL.revokeObjectURL(elink.href) // 釋放URL 對(duì)象document.body.removeChild(elink)} else { // IE10+下載fileName ? navigator.msSaveOrOpenBlob(blob, fileName) : navigator.msSaveOrOpenBlob(blob)} }需注意請(qǐng)求方式:
function filePost(data, url) {if (data) {data = paramsToFormData(data)}return new Promise((re, rj) => {axios({method: 'post',url: url,async: false,data: data || '',responseType: 'blob' // 導(dǎo)出文件相應(yīng)類型}).then((res) => {re(res.data)})}) }總結(jié)
以上是生活随笔為你收集整理的js post请求下载文件的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: java提示系统找不到指定路径怎么解决?
- 下一篇: 呷哺呷哺的中年危机与贺光启的囚徒困境