亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Vue.js,下載后動態顯示圖像

Vue.js,下載后動態顯示圖像

慕慕森 2023-10-10 16:20:56
我正在 Vue.js / Node.js (express.js) 中工作,并嘗試在通過 GET 請求(由 Axios)從服務器下載圖像后顯示圖像。該文件是由用戶上傳的,因此我們不知道它的詳細信息(長度等)。并且下載后需要展示給用戶。服務器收到下載請求后,只需從磁盤獲取文件并將其放入響應中:(req, res) => {    ...    res.download(file)}到目前為止,我在客戶端中執行了此操作,下載文件后,我將其轉換為 Base64 字符串:Buffer.from(response.data, 'binary').toString('base64')(GET 請求的響應)并嘗試在我的模板上顯示它,如下所示::src="'data:image/png;base64,' + myBase64string"而且根本沒有成功!非常感謝任何建議、建議、幫助等。
查看完整描述

2 回答

?
躍然一笑

TA貢獻1826條經驗 獲得超6個贊

new Vue({

  el: '#app',

  

  data () {

    return {

      src: null

    }

  },

 

  mounted () {

    let self = this

    fetch("https://thumbs.dreamstime.com/z/freely-accessible-examination-exposition-mosaic-ar-figures-mosaic-st-petersburg-russia-june-freely-accessible-119320551.jpg")

    .then((response) => {

      response.blob().then(blobResponse => {

        let reader = new FileReader();

        reader.readAsDataURL(blobResponse); 

        reader.onloadend = function() {

          let base64data = reader.result; 

          let img = document.createElement('img')

          img.src = base64data  

          self.$refs['img-container'].appendChild(img)

        }

      })

    })

  }

})

img {

  height: 300px;

}

<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>


<div id="app">

  <div ref="img-container"></div>

</div>


查看完整回答
反對 回復 2023-10-10
?
aluckdog

TA貢獻1847條經驗 獲得超7個贊

請求的響應類型(axios請求)應該是?responseType: 'blob'
從服務器接收到數據后,我簡單地使用下面這行代碼生成一個DOM字符串,然后將其作為圖像標簽的src:

const?imageUrl?=?window.URL.createObjectURL(new?Blob([response.data]))

<img?:src="imageUrl?">


查看完整回答
反對 回復 2023-10-10
  • 2 回答
  • 0 關注
  • 176 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號