是否可以将 html 代码和所有图像放在一个文件中?
我想要一个带有 javascript 的 html 文件。然后我想在这个文件中包含一些图像。我想将此 html 文件发送给我的朋友(通过电子邮件)。我希望他们看到我的带有图像的 html 文件,但我不想向他们发送带有所有图像的所有文件。如果只向他们发送一份文件就好了。
我也不想在网络服务器上放置图像。
我也不想向他们发送包含所有文件的存档(因为他们需要打开该存档)。
我想要的太多还是可以做我想做的事?
添加
我不希望我的朋友在邮件客户端中看到该 html 文件。我想以附件形式发送一个文件。因此,他们可以保存它,然后用浏览器打开。
I want to have a html file with javascript. Then I want to have some images in this file. I want to send this html file to my friends (per e-mail). I want them to see my html file with images but I do not want to send them all files with all images. It would be nice to send them just one file.
I also do not want to have images on a web-server.
I also do not want to send them an archive with all the files (since they then need to open this archive).
Do I want to much or it's possible to do what I want?
ADDED
I do not want my friends to see the html file in a mail-client. I want to send a file as an attachment. So, they can save it and then open with a browser.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
是的,这是可能的:
文件源使用 Base64 算法进行编码,可以轻松地正常表示二进制数据文本。
在维基百科上了解更多信息:数据 URI 方案。
Yes, it is possible:
File source is encoded using Base64 algorithm that allows easily represent binary data as normal text.
Find out more on wikipedia: Data URI scheme.
根据邮件客户端是否支持,理论上您可以使用 数据 URI 方案,例如所以:
同样,支持取决于邮件客户端。有些人可能根本不支持。有些可能会在 X 个字节后截断。等等。据我所知,这样的人并不多。此外,我没有看到像这样在 HTML 中内嵌图像的其他方法。在广泛支持之前,最好的选择是将图像作为附件发送。
更新按照OP的更新:好吧,大多数现代网络浏览器都支持它。前面提到的维基百科链接甚至详细提到了它们。
请注意,IE8 会在 32KB 之后截断字符串。因此,只要图像不是那么大,您就可以为 IE8 用户使用数据 URI 方案。 IE7 及更低版本不支持。
Depending on whether the mail client supports it, you could in theory use the data URI scheme, like so:
Again, the support is mail client dependent. Some might not support it at all. Some might truncate after a X amount of bytes. Etcetera. As far as I know there aren't many of them. Further I don't see another ways to inline images in HTML like that. Until the support is widespread, your best bet is really to send the images along as an attachment.
Update as per the OP's update: well, most of the modern webbrowsers supports it. The aforementioned Wikipedia link even mentions them in detail.
Note that IE8 truncates the string after 32KB. So, as long as the images aren't that large, you could use the data URI scheme for IE8 users. It's not supported on IE7 and lower.
我不知道有什么方法可以 100% 确定地实现您所追求的目标。
有没有办法放弃图像?也许是 ascii 表示形式? (类似于 http://www.text-image.com/)
存档将是我所知道的唯一的“单个文件”选项。
I am not aware of a way to accomplish what you're after with 100% certainty it will work.
Is there a way to forgo the images? Perhaps an ascii representation instead? (something like this http://www.text-image.com/)
The archive would be the only "single file" option that I'm aware of.
您无法从邮件客户端执行 javascript。您可以内联图像,但您将需要一个库,因为手动操作并不简单。
您应该向他们发送一个链接。
You cant execute javascript from a mail client. You can inline the images, but you will need a library because doing it by hand is non-trivial.
You should just send them a link.
为什么不直接使用相对路径链接图像,并将它们与 html 文件捆绑在一个文件夹中,然后将其存档并压缩(zip 或 tarball,取决于偏好)发送?
Why don't you just link the images with relative paths, and bundle them in a folder with the html file and send it archived and compressed (zip or tarball, depending on preference)?
如果您只想发送一个文件,只需使用您最喜欢的压缩程序对其进行压缩即可。
If you just want to send one file, just zip it using your favorite compression program.
在任何情况下,您都不应该发送正文为 HTML 的电子邮件。发送带有图像作为 MIME 附件的纯文本邮件,或者更好的是,将图像放在网站上(我听说 Flickr 非常好;-)并向它们发送 URL。
我要再说一遍,因为需要更频繁地说:电子邮件必须是纯文本。
You should never, under any circumstances, send email whose body is HTML. Send plain text mail with the images as MIME attachments, or better yet, put the images on a website (I hear Flickr is quite good ;-) and send them URLs.
I'm going to say it again, because it needs to be said more often: email must be plain text.