如果它位于单独的文件中,如何在 HTML 中显示 base64 编码的图像? [英] How to display base64 encoded image in HTML if it is located in a separated file?

查看:33
本文介绍了如果它位于单独的文件中,如何在 HTML 中显示 base64 编码的图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 base64 编码的图像.如果我把它放到 html 中,它就可以工作:

I have base64 encoded image. If I put it right into html it works:

<img src="data:image/png;base64,..."/>

但是当我将所有 base64 内容放入一个单独的文件时,它不会:

But when I put all that base64 content into a separated file, it doesn't:

<img src="image.base64.txt"/>

我尝试将扩展名更改为 .png,但没有帮助.有什么想法吗?

I tried changing extension to .png, but it doesn't help. Any ideas?

推荐答案

您需要随文件一起发送正确的 Content-type、Content-encoding 和 charset HTTP 标头.请注意,它们都是 data: URI 模式的一部分.你真的应该在内容类型和编码之间有一个 charset=utf-8 或类似的子句:

You would need to send the correct Content-type, Content-encoding and charset HTTP headers along with the file. Note that they are all part of the data: URI schema as well. You really should have a charset=utf-8 or similar clause between the content-type and the encoding:

url(data:image/png;charset=utf-8;base64,...);

这篇关于如果它位于单独的文件中,如何在 HTML 中显示 base64 编码的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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