reactjs - 在反应中获取和显示文件
问题描述
我正在学习反应,我正在尝试从 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()
}
有人可以检查一下吗,我试图找到解决方案,但我被卡住了。
谢谢
解决方案
推荐阅读
- php - php类常量或类构造错误?
- typed-racket - 你应该如何在 Typed Racket 的 letrec 中键入注解相互递归函数?
- javascript - 同一类中的 Javascript 调用方法未定义?
- wagtail - 将 Django 应用程序添加到现有 Wagtail 站点 - django-filer
- mysql - MySQL Event Scheduler:存档超过 1 天的数据
- javascript - 使用 Flask 和静态路径链接 .js 文件
- ios - 不要在 WKWebView iOS 中显示 ImagePicker
- excel - CountIfs() Power Query M 中的等效项,自身内的每行计数
- java - 如果在循环的迭代过程中输入了无效的输入,我如何让循环在其当前迭代中重新开始
- r - 如何在 ggplot2 中映射 10 多种不同的线型