vue+axios將後端返回的圖片流顯示到img標籤中


      var that = this
      axios
        .get("接口地址", {
          responseType: "arraybuffer",
          params: 傳給後端的數據
        })
        .then(response => {
          return (
            "data:image/png;base64," +
            btoa(
              new Uint8Array(response.data).reduce(
                (data, byte) => data + String.fromCharCode(byte),
                ""
              )
            )
          );
        })
        .then(data => {
          this.imgUrl = data; //賦值給img標籤的src屬性
        });

費了好大勁,剛開始用什麼blob結果格式不對,又直接寫base64的也不對,直接賦值更不對了。

上面的方法可以,親測有效,responseType: "arraybuffer",很重要哦

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章