【问题标题】:Image is not saved while creating a html file创建 html 文件时不保存图像
【发布时间】:2015-02-23 19:07:59
【问题描述】:

我正在使用 twitter bootstrap 开发一个 .NET 应用程序。

我正在 .aspx 页面中加载图像并将该页面保存为 html 文件。

这是我的代码:

protected void Page_Load(object sender, EventArgs e)
{
        string ISMfilePath = Server.MapPath("~/Download/");
        string FileNamePath = @D:"/Export.html";
        StreamWriter sWriter = File.CreateText(FileNamePath);
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        pnlData.RenderControl(htw);
        sWriter.WriteLine(sw.ToString());
        sWriter.Close();
}
<asp:Panel  ID="pnlData"  runat="server" class="pnlData" style="background-   color:White;">
<img src="mountain.jpg" alt="" style="width:128px;height:128px"/>
</asp:Panel>

我能够创建一个 html 文件,但 mountain.jpg 没有加载到 html 页面中。图片未保存在 html 页面中。

【问题讨论】:

  • 您无法将图像保存到 html 页面中。你从那里加载链接。我想你可能想重新考虑你在做什么。
  • 您希望如何将 jpg 文件保存在纯 html 中?作为数据uri?

标签: javascript asp.net twitter-bootstrap


【解决方案1】:

不要将图像 src 作为文件名,而是使用 base64 字符串。这会将图像嵌入到 html 中,并为您提供所需的内容。

例如

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

您可以使用http://www.base64-image.de/等各种网站将图像转换为base64字符串

更多信息请参见http://en.wikipedia.org/wiki/Data_URI_scheme

仅供参考:只记得 IE8 仅支持 base64 字符串作为图像小于 32kb 的数据 URI,但它是我见过的唯一存在该问题的浏览器。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-12
    • 2020-10-24
    • 2012-09-28
    • 1970-01-01
    • 2015-10-29
    • 1970-01-01
    • 2014-04-26
    相关资源
    最近更新 更多