简体   繁体   English

如何将图片Blob网址读取或转换为图片?

[英]How to read or convert an image blob url into an image?

I have an image that is stored in local storage. 我有一个存储在本地存储中的图像。 On load the image is displayed. 加载时显示图像。

componentDidMount() {
 const reader = new FileReader();
 reader.onload = e => {
  this.setState({ blob: e.target.result });
 };
 reader.readAsDataURL(this.props.file);
}
render() {
 return (
  <div>
   <img src={this.state.blob} />
  </div>
 )
}

The file object looks like this: 文件对象如下所示:

lastModified:1535424554987
name:"test.jpeg"
preview:"blob:http://localhost:8080/b52098ca-087f83c778f0"
size:41698
type:"image/jpeg"
webkitRelativePath:""

When I refresh the page and try to load the preview again, it doesn't display the image, and the file object looks different: 当我刷新页面并尝试再次加载预览时,它不显示图像,并且文件对象看起来不同:

{preview: "blob:http://localhost:8080/b52098ca-087f83c778f0"}

Is it possible to read the image from this url? 是否可以从该网址读取图像? Or is it impossible to render the image without the full file object? 还是没有完整的文件对象就不可能渲染图像?

blob URL is temporary. Blob URL是临时的。 if you reload the web page, the blob URL will gone. 如果您重新加载网页,则Blob URL将消失。
If you want to save the image in localStorage you can save it as base64 data. 如果要将映像保存在localStorage中,则可以将其另存为base64数据。

But if your image is too big localStorage won't suit because it has limits which depend on the browser from 5MB to 10MB. 但是,如果您的映像太大,则localStorage不适合,因为它的限制取决于浏览器,从5MB到10MB。

据我所知,将文件保存到localStorage或从中保存是不可能的,这是因为它对数据运行JSON.stringify。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM