我动态生成了一个 html 文档,然后将其下载为带有 .doc 扩展名的 word 文件。问题是,在我的机器(本地主机)上,徽标图像正在成功显示,但是当我将代码上传到服务器时,图像没有显示。我在服务器上的图像 src 中没有发现任何问题,我也没有在服务器上安装 ms-office,但在我的机器上我安装了 ms-office。下面是我生成word文件的代码。请检查一下,如果你能解决我的问题,请告诉我。提前致谢。
public void DownloadWordFormat(string id)
{
ViewModel model = FetchContent(id);
string fileName = Guid.NewGuid().ToString() + ".html";
string path = MyConfiguration.Instance.DirectoryToDownloadFile + fileName;
string savePath = Request.MapPath(path);
string logoPath = Request.MapPath(GetLogo());
StringBuilder sb = GenerateHTMLForWordDocument(model, logoPath);
// Create the file.
using (FileStream fs = System.IO.File.Create(savePath, 1024))
{
Byte[] info = new UTF8Encoding(true).GetBytes(sb.ToString());
// Add some information to the file.
fs.Write(info, 0, info.Length);
}
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.Buffer =true;
Response.WriteFile(savePath);
Response.ContentType="application/msword";
string file = model.SessionId + ".doc";
Response.AddHeader("Content-Disposition", "attachment; filename=" + file);
Response.Flush();
Response.End();
// Delete the file if it exists.
if (System.IO.File.Exists(savePath))
{
System.IO.File.Delete(savePath);
}
}
和
private static StringBuilder GenerateHTMLForWordDocument(ViewModel model,string logo)
{
StringBuilder sb = new StringBuilder();
sb.Append("<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:w='urn:schemas-microsoft-com:office:word' xmlns='http://www.w3.org/TR/REC-html40'> ");
sb.Append("<head>");
sb.Append("<title></title>");
sb.Append("<!--[if gte mso 9]><xml> <w:WordDocument> <w:View>Print</w:View> <w:Zoom>90</w:Zoom></w:WordDocument></xml><![endif]-->");
sb.Append("<style>");
sb.Append("p.MsoFooter, li.MsoFooter, div.MsoFooter");
sb.Append("{");
sb.Append("margin:0in;margin-bottom:.0001pt;mso-pagination:widow-orphan;tab-stops:center 3.0in right 6.0in;font-size:12.0pt;");
sb.Append("}");
sb.Append("<style>");
sb.Append("<!-- /* Style Definitions */");
sb.Append("@page Section1");
sb.Append("{");
sb.Append("size:8.5in 11.0in; margin:1.0in 1.0in 1.0in 1.0in;mso-header-margin:.5in;mso-footer-margin:.5in;mso-title-page:yes; mso-header: h1;mso-footer: f1;mso-first-header: fh1;mso-first-footer: ff1;mso-paper-source:0;");
sb.Append("}");
sb.Append("div.Section1");
sb.Append("{");
sb.Append("page:Section1;");
sb.Append("}");
sb.Append("table#hrdftrtbl");
sb.Append("{");
sb.Append("margin:0in 0in 0in 900in;width:1px;height:1px;overflow:hidden;");
sb.Append("}");
sb.Append("-->");
sb.Append("</style></head>");
sb.Append("<body lang=EN-US style='tab-interval:.5in'>");
sb.Append("<div class=Section1>");
sb.Append(model.Content);
sb.Append("<table id='hrdftrtbl' border='0' cellspacing='0' cellpadding='0'>");
sb.Append("<tr><td>");
sb.Append("<div style='mso-element:header' id=h1 >");
sb.Append("<div class=MsoHeader >");
sb.Append("<table width='100%'>");
sb.Append("<tr><td><img src='" + logo + "' alt='logo' /></td><td style='vertical-align:top'><span>" + model.Session + " (" + model.SessionId + ")</span></td></tr>");
sb.Append("<tr><td colspan='2'><hr /><br /></td></tr>");
sb.Append("</table>");
sb.Append("</div>");
sb.Append("</div>");
sb.Append("");
sb.Append("</td><td>");
sb.Append("<div style='mso-element:footer' id=f1>");
sb.Append("<p class=MsoFooter>");
sb.Append(" <span style=mso-tab-count:2'></span>");
sb.Append("<table width='100%'>");
sb.Append("<tr><td colspan='2'><hr /></td></tr>");
sb.Append("<tr><td align='left'>Page <span style='mso-field-code: PAGE '><span style='mso-no-proof:yes'></span></span> of <span style='mso-field-code: NUMPAGES '></span></td>");
sb.Append("<td align='right'>Downloaded on: <span> " + DateTime.Now.ToString("dd MMM yyyy h:MM tt") + "</span></td></tr>");
sb.Append("</table></p></div>");
sb.Append("<div style='mso-element:header' id=fh1 >");
sb.Append("<div class=MsoHeader >");
sb.Append("<table width='100%'>");
sb.Append("<tr><td><img src='" + logo + "' alt='logo' /></td><td style='vertical-align:top'><span>" + model.Session + " (" + model.SessionId + ")</span></td></tr>");
sb.Append("<tr><td colspan='2'><hr /><br /></td></tr>");
sb.Append("</table>");
sb.Append("</div></div>");
sb.Append("<div style='mso-element:footer' id=ff1>");
sb.Append("<p class=MsoFooter><span lang=EN-US style='mso-ansi-language:EN-US'>");
sb.Append(" <table width='100%'>");
sb.Append("<tr><td colspan='2'><hr /></td></tr>");
sb.Append("<tr><td align='left'>Page <span style='mso-field-code: PAGE '><span style='mso-no-proof:yes'></span></span> of <span style='mso-field-code: NUMPAGES '></span></td>");
sb.Append("<td align='right'>Downloaded on: <span>" + DateTime.Now.ToString("dd MMM yyyy h:MM tt") + "</span></td></tr>");
sb.Append("</table>");
sb.Append("<o:p></o:p></span></p>");
sb.Append("</div>");
sb.Append("</td></tr>");
sb.Append("</table>");
sb.Append("</body></html>");
return sb;
}
private static string GetLogo()
{
string logo = Links.Areas.MAL.Content.Images.LOGO_png;
logo = logo.Substring(0, logo.LastIndexOf("?"));
return logo;
}