Header image is not being displaying at the time o

2019-09-05 10:26发布

问题:

I have generated an html document on the fly and later downloading it as word file with .doc extension. The problem is that on my machine (localhost) logo image is being displaying successfully but when I uploaded my code on server then image is not displaying. I did not find any problem in image src on server also I don't have ms-office installed on server but on my machine I have ms-office installed. Below is my code to generate word file. Please check it and let me know if you can solve my problem. Thanks in advance.

     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);
        }


    }

and

     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;
    }

回答1:

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



回答2:

I got the solution of this problem. Actually the problem was the way I was picking the logo image. In my above code the line to get the image path is like this:

Request.MapPath(GetLogo());

Here problem is that Request.MapPath() maps the specified virtual path to a physical path. So at the time when word document was being downloaded on client machine it was looking for the header logo on the path "C:\XYZ\Dev\VNext\Scs.root\Scs\WWW\Areas\OnlineShoping\Content\Images\LOGO.png" and because of logo image exist at server not on client machine from this path that is why it was not being added in the word document at the of downloading. My problem got solved when I gave absolute path like this:

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

So the above code generates word document successfully with .doc extension having html data as input. Also this code does not required ms-office or ms-word installed on server.