vue?下載文檔亂碼的解決
vue下載文檔亂碼
最近寫功能 vue導(dǎo)出,但是不知道為啥,一請求接口就是亂碼
后來在接口里寫上了 這句話 responseType:“blob”,
能下載了趕快高興打開一看 日,下載下來的文件里面又是亂碼
后來不停的琢磨,咦終于找到方法了
這里面加了一句話 終于成功了!
我給大家把代碼貼上
exportAccountApi(data).then(res=>{ console.log('777666',res) const blob = new Blob([res],{type: "application/vnd.ms-excel"}); let fileName = "存款記錄明細.xls"; if ("download" in document.createElement("a")) { 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); document.body.removeChild(elink); }else{ navigator.msSaveBlob(blob.fileName) } })
文件下載返回亂碼處理 vue+axios
后端返回數(shù)據(jù)流是亂碼,可以使用new Blob()這個方法處理,可以解決亂碼問題。
亂碼返回結(jié)果如下:
解決方法
async postClick() { const res = await axios({ url: '后端接口', method: 'post', data: { id: '文件id' } responseType: 'blob' }) const content = res.data const fileName = 'a.png' // 文件名稱 // 如果不確定文件類型,type可以寫空字符串 const bolb = new Blob([content], { type: '' }) if ('download' in document.createElement('a')) { const link = document.createElement('a') link.download = fileName link.style.display = 'none' // URL.createObjectURL(bolb) = blob:http://localhost:8080/a34a8a20-acf2-3f21-bc22-45994d9f0290 link.href = URL.createObjectURL(bolb) document.body.appendChild(link) link.click() URL.revokeObjectURL(link.href) document.body.removeChild(link) } }
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
vuex結(jié)合session存儲數(shù)據(jù)解決頁面刷新數(shù)據(jù)丟失問題
在項目中表單篩選項里,選擇完之后刷新頁面數(shù)據(jù)就變了,沒有保留在自己選擇的選項上。本文使用session存儲數(shù)據(jù),具有一定的參考價值,感興趣的可以了解一下2021-09-09ant-design-vue中的table自定義格式渲染解析
這篇文章主要介紹了ant-design-vue中的table自定義格式渲染,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10