我正在尝试在进行 JavaScript 调用后获取要在 React 应用程序中显示的图像(PNG 格式)。代码如下。函数DeviceService.getFile以 blob 形式返回文件。数据是二进制的。如何在 React 中正确显示此图像?
我已经尝试转换为 base64,但没有帮助。
DeviceService.getFile(mapOverlayImagePath, bMap1 => {
this.setState({ MapOverlay: bMap1 })
// this.setState({ MapOverlay: 'data:image/png;base64,' + btoa(bMap1) })
console.log(bMap1)
})
显示图像的 React 代码:
<img src={this.state.MapOverlay} alt="MapOverlay" />
我修改了这个函数,函数getFile如下:
export function getFile(path, cb) {
if (typeof(cb) === 'undefined' || cb === null)
return;
fetch(uris.getFile() + '/?' +
'path=' + path,
{method: 'POST', credentials: 'include'})
.then(reply => reply.blob())
.then((response) => {
if (response.data) {
return cb(response.data);
}
return cb(new Blob());
})
}
这个 getFile 函数位于一个库中,它在 React 应用程序中用作依赖项。
我尝试在 Internet Explorer 控制台中打印 blob 大小并显示:[object Blob]: {size: 0, type: ""}. 我想我的 getFile 函数没有按预期传递数据。getFile 函数中的任何错误?