问题
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