HLJ 发布于
2022-10-03 12:12:10

fetch请求图片获取图片的大小和blob数据

<template>
  <div id="progressBar_max"></div>
</template>

<script>
export default {
  created: function () {
    this.test()
  },
  methods: {
    async test() {
      let response = await fetch('good4/test/1.jpg');

      let blob = await response.blob(); // 下载为 Blob 对象
      const progressBar_max = document.getElementById("progressBar_max");

      progressBar_max.innerHTML = `${(blob.size / 1024 / 1024).toFixed(3)}MB`;

      // 为其创建一个 <img>
      let img = document.createElement('img');
      img.style = 'width:100px';
      document.body.append(img);

      // 显示它
      img.src = URL.createObjectURL(blob);

    },
  }
}
</script>

https://zh.javascript.info/fetch

当前文章内容为原创转载请注明出处:http://www.good1230.com/detail/2022-10-03/609.html
最后生成于 2024-03-23 10:54:30
此内容有帮助 ?
0