首页 > 解决方案 > 在反应中获取和显示文件

问题描述

我正在学习反应,我正在尝试从 retsfull API 获取图像文件并显示图像。我正在将响应文件转换为 blob 格式,并尝试使用文件阅读器读取它,然后将结果设置在状态变量上,当我这样做时,我得到了一个损坏的图像。下面是代码:

getData() {
    var xhr = new XMLHttpRequest();
    xhr.addEventListener('load', () => {
      var myblob = new Blob([xhr.response], {
        type: ["image/png", "image/jpeg", "image/gif"]
      });
      let reader = new FileReader()
      reader.onloadend = () => {
        this.setState({
          postPicture: myblob,
          postPicturePreview: reader.result
        })
      }
      reader.readAsDataURL(myblob)
    })
    xhr.open('GET','https://DigitalSelfassuredCrash.kirilkuzmanovsk.repl.co')
    xhr.send()
    }

有人可以检查一下吗,我试图找到解决方案,但我被卡住了。

谢谢

标签: reactjsexpress

解决方案


推荐阅读