Can a html img tag read base64 files?

折月煮酒 提交于 2020-02-05 05:38:06

问题


Short question: Can a html img tag read a base64 encoded image file?

<img src='path-to-file/image.base64'>

Because I'm having trouble. It fires the onerror event but doesn't give a reason (well at least it doesn't say it couldn't find the file)

Oh! Some context: I'm trying to cache images to sdcard on android as base64 encoded files. That part works, but when I try to load the cached image it fails :(


回答1:


The syntax to source the image from inline data is:

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA
AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO
9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="your image">



回答2:


Okay it turns out that base64 is only for inline use (or if you have the string you can set it with javascript)

So this works:

var image = new Image();
image.src = base64_string;

But it doesn't work when you point the src to a file containing the base64_string

var image = new Image();
image.src = "path/base64.txt";

PS: Thanks for the comments, this pointed me in the right direction



来源:https://stackoverflow.com/questions/25968358/can-a-html-img-tag-read-base64-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!