HTML img标签可以读取base64文件吗? [英] Can a html img tag read base64 files?

查看:1629
本文介绍了HTML img标签可以读取base64文件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简短的问题:html img标签可以读取base64编码的图像文件吗?

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

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

因为我遇到了麻烦.它会触发onerror事件,但没有给出原因(至少它没有说找不到文件)

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)

哦!一些上下文:我正在尝试将图像作为base64编码的文件缓存到android上的sdcard.那部分工作,但是当我尝试加载缓存的图像时,它会失败:(

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 :(

推荐答案

好吧,事实证明base64仅用于内联使用(或者,如果您具有字符串,则可以使用javascript进行设置)

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

这可行:

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

但是当您将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:感谢您的评论,这为我指明了正确的方向

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

这篇关于HTML img标签可以读取base64文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆