vue二进制转图片显示问题, 后端返回的是byte[]数组

<template>
  <!-- <router-view></router-view> -->
  <div id="img">
    <img :src="url" alt="" style="width: 100px;">
  </div>
</template>

<script setup>
import { ref } from "vue";
import axios from "axios";
const url = ref('')
axios.get("/api/stock/exportOrderManagement/exportOrder/1").then((res) => {
  var arry = res.data.obj.importerSignature;
  var str12 = arrayBufferToBase64(arry); //转换字符串
  const imgUrl = "data:image/png;base64," + str12
  url.value = imgUrl
});

function 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);
}

</script>

渲染结果:

posted @ 2023-06-16 17:08  jscook  阅读(871)  评论(0)    收藏  举报