Vue拿到二進制流圖片如何轉(zhuǎn)為正常圖片并顯示
更新時間:2022年06月05日 15:05:42 作者:pretty_girls
這篇文章主要介紹了Vue拿到二進制流圖片如何轉(zhuǎn)為正常圖片并顯示,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
二進制流圖片轉(zhuǎn)為正常圖片并顯示
第一步
axios({ ? ? ? ? method: 'get', ? ? ? ? url, ? ? ? ? responseType: 'arraybuffer' // 最為關鍵 ? ? ? }) ? ? ? ? .then(function (response) { ? ? ? ? ? that.src = 'data:image/jpeg;base64,' + that.arrayBufferToBase64(response.data) ? ? ? ? })
arrayBufferToBase64 (buffer) { ? ? ? var binary = '' ? ? ? var bytes = new Uint8Array(buffer) ? ? ? var len = bytes.byteLength ? ? ? for (var i = 0; i < len; i++) { ? ? ? ? binary += String.fromCharCode(bytes[i]) ? ? ? } ? ? ? return window.btoa(binary) ? ? },
?<img :src="src" alt="驗證碼">
解析blob 二進制流圖片的展示
<el-image v-loading="loading" style='height: 480px;' :src="imgsrc"></el-image>
// /api/plan.js文件請求方法 export function getFlowPhoto(data) { ? ? return request({ ? ? ? ? url: '/xxx/xxxx/getFlowPhoto', ? ? ? ? method: 'post', ? ? ? ? responseType: 'blob', ? ? ? ? data ? ? }) }
<script> ? import {getFlowPhoto} from "@/api/plan.js"; ? export default { ? ? data() { ? ? ? return { ? ? ? ? imgsrc:'', ? ? ? ? loading:false, ? ? ? } ? ? }, ? ? mounted() {}, ? ? methods: { ? ? ? fetchData() { ? ? ? ? this.loading = true; ? ? ? ? var that = this; ? ? ? ? getFlowPhoto({id:xxx}).then((res) => { ? ? ? ? ? if(res.code == 401){ ? ? ? ? ? ? this.$message({ ? ? ? ? ? ? ? message: res.message, ? ? ? ? ? ? ? type: "error", ? ? ? ? ? ? }); ? ? ? ? ? } ? ? ? ? ? if(res){ ? ? ? ? ? ? const myBlob = new window.Blob([res], {type: 'image/jpeg'}) ? ? ? ? ? ? const qrUrl = window.URL.createObjectURL(myBlob) ? ? ? ? ? ? this.imgsrc = qrUrl; ? ? ? ? ? ? this.loading = false; ? ? ? ? ? } ? ? ? ? }); ? ? ? }, ? ? } ? } </script>
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
vue中el-date-picker type=daterange日期清空時不回顯的解決
這篇文章主要介紹了vue中el-date-picker type=daterange日期清空時不回顯的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07解決Ant Design Modal內(nèi)嵌Form表單initialValue值不動態(tài)更新問題
這篇文章主要介紹了解決Ant Design Modal內(nèi)嵌Form表單initialValue值不動態(tài)更新問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10Vue.js + Nuxt.js 項目中使用 Vee-validate 表單校驗
vee-validate 是為 Vue.js 量身打造的表單校驗框架,允許您校驗輸入的內(nèi)容并顯示對應的錯誤提示信息。這篇文章給大家?guī)砹薞ue.js 使用 Vee-validate 實現(xiàn)表單校驗的相關知識,感興趣的朋友一起看看吧2019-04-04