0

我动态生成了一个 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 + "&nbsp;(" + 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 + "&nbsp;(" + 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;
    }
4

2 回答 2

0

make sure its pre rendered, my code is in asp.net c#(not mvc) but if it helps you can try it:

 public override void VerifyRenderingInServerForm(Control control)
    {
        /* Verifies that the control is rendered */

    }

Regards

于 2013-04-30T21:04:46.617 回答
0

我得到了这个问题的解决方案。实际上问题在于我选择徽标图像的方式。在我上面的代码中,获取图像路径的行是这样的:

Request.MapPath(GetLogo());

这里的问题是Request.MapPath() 将指定的虚拟路径映射到物理路径。因此,当在客户端计算机上下载 word 文档时,它正在寻找路径“C:\XYZ\Dev\VNext\Scs.root\Scs\WWW\Areas\OnlineShoping\Content\Images\LOGO”上的标题徽标.png" 并且由于徽标图像存在于服务器上而不是来自该路径的客户端计算机上,这就是为什么它没有在下载时添加到 word 文档中的原因。当我给出这样的绝对路径时,我的问题得到了解决:

string path="http://abc.com/Areas/Content/Images/LOGO.png"

因此,上面的代码成功地生成了带有 .doc 扩展名的 word 文档,并将 html 数据作为输入。此外,此代码不需要在服务器上安装 ms-office 或 ms-word

于 2013-05-02T08:24:36.263 回答