vue實現(xiàn)圖片路徑轉(zhuǎn)二進制文件流(binary)
圖片路徑轉(zhuǎn)二進制文件流(binary)
vue項目中,需要將本地的圖片或前端生成的圖片傳回后端,傳回給后端需要將圖片路徑轉(zhuǎn)換為二進制文件流,也就是參數(shù)中顯示的(binary),這時就需要進行圖片路徑的轉(zhuǎn)換。
圖片路徑轉(zhuǎn)換為Base64
imageUrlToBase64(imageUrl) { ? let image = new Image() // 一定要設(shè)置為let,不然圖片不顯示 ? image.setAttribute('crossOrigin', 'anonymous') // 解決跨域問題 ? image.src = imageUrl +"&v=" + Math.random() ? image.onload = () => { ? ? var canvas = document.createElement("canvas") ?? ?canvas.width = image.width ?? ?canvas.height = image.height ?? ?var context = canvas.getContext('2d') ?? ?context.drawImage(image, 0, 0, image.width, image.height) ?? ?var quality = 0.8 ?? ?var dataURL = canvas.toDataURL("image/jpeg", quality) // 使用toDataUrl將圖片轉(zhuǎn)換成jpeg的格式,不要把圖片壓縮成png,因為壓縮成png后base64的字符串可能比不轉(zhuǎn)換前的長! ? ? this.base64toFile(dataURL) ? } }
Base64轉(zhuǎn)換為二進制文件流(binary)
base64toFile (dataurl, filename = 'file') { ? let arr = dataurl.split(',') ? let mime = arr[0].match(/:(.*?);/)[1] ? let suffix = mime.split('/')[1] ? let bstr = atob(arr[1]) ? let n = bstr.length ? let u8arr = new Uint8Array(n) ? while (n--) { ? ? u8arr[n] = bstr.charCodeAt(n) ? } ? let file = new File([u8arr], `${filename}.${suffix}`, { ? ? type: mime ? }) ? console.log(file) }
下載二進制流文件
平時在前端下載文件有兩種方式,一種是后臺提供一個 URL,然后用 window.open(URL) 下載;另一種是后臺直接返回文件的二進制內(nèi)容,然后前端轉(zhuǎn)化再下載,下面主要說的是第二種實現(xiàn)方式
Blob、ajax(axios)
mdn 上是這樣介紹 Blob 的:
Blob 對象表示一個不可變、原始數(shù)據(jù)的類文件對象。Blob 表示的不一定是JavaScript原生格式的數(shù)據(jù)
具體使用
axios({ ? method: 'post', ? url: '/export', }) .then(res => { ? // 假設(shè) data 是返回來的二進制數(shù)據(jù) ? const data = res.data ? const url = window.URL.createObjectURL(new Blob([data], {type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"})) ? const a = document.createElement('a') ? a.href = url ? a.click() ? // const link = document.createElement('a') ? // link.style.display = 'none' ? // link.href = url ? // link.setAttribute('download', 'excel.xlsx') ? // document.body.appendChild(link) ? // link.click() ? // document.body.removeChild(link) })
打開下載的文件,一堆亂碼…一定有哪里不對。
最后發(fā)現(xiàn)是參數(shù) responseType 的問題,responseType 它表示服務(wù)器響應的數(shù)據(jù)類型,由于后臺返回來的是二進制數(shù)據(jù),所以我們要把它設(shè)為 arraybuffer, 接下來再看看結(jié)果是否正確。
axios({ ? method: 'post', ? url: '/export', ? responseType: 'arraybuffer', }) .then(res => { ? // 假設(shè) data 是返回來的二進制數(shù)據(jù) ? const data = res.data ? const url = window.URL.createObjectURL(new Blob([data], {type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"})) ? const a = document.createElement('a') ? a.href = url ? a.click() ? // const link = document.createElement('a') ? // link.style.display = 'none' ? // link.href = url ? // link.setAttribute('download', 'excel.xlsx') ? // document.body.appendChild(link) ? // link.click() ? // document.body.removeChild(link) })
這次沒有問題,文件能正常打開,內(nèi)容也是正常的,不再是亂碼。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue路由守衛(wèi)及路由守衛(wèi)無限循環(huán)問題詳析
這篇文章主要給大家介紹了關(guān)于vue路由守衛(wèi)及路由守衛(wèi)無限循環(huán)問題的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用vue具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-09-09解決vue中修改了數(shù)據(jù)但視圖無法更新的情況
今天小編就為大家分享一篇解決vue中修改了數(shù)據(jù)但視圖無法更新的情況,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08vue項目之webpack打包靜態(tài)資源路徑不準確的問題
這篇文章主要介紹了vue項目之webpack打包靜態(tài)資源路徑不準確的問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-04-04ElementUI下拉組件el-select一次從后端獲取選項并設(shè)置默認值方式
這篇文章主要介紹了ElementUI下拉組件el-select一次從后端獲取選項并設(shè)置默認值方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-01-01Vue2?this?能夠直接獲取到?data?和?methods?的原理分析
這篇文章主要介紹了Vue2?this能夠直接獲取到data和methods的原理分析,因為methods里的方法通過bind指定了this為new?Vue的實例2022-06-06vue跳轉(zhuǎn)頁面并且實現(xiàn)參數(shù)傳遞接受示例
這篇文章主要為大家介紹了vue跳轉(zhuǎn)頁面并且實現(xiàn)參數(shù)傳遞接受示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-06-06