0

I'm trying to generate a word document using StringBuilder, as follows

    [WebMethod]
    public static void ExportToWord(string HTMLContent)
    {

        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.Charset = "";
        HttpContext.Current.Response.ContentType = "application/msword";
        string strFileName = "GenerateDocument" + ".doc";
        HttpContext.Current.Response.AddHeader("Content-Disposition", "inline;filename=" + strFileName);
        System.Text.StringBuilder strHTMLContent = new System.Text.StringBuilder();
        strHTMLContent.Append(HTMLContent);


        HttpContext.Current.Response.Write(strHTMLContent);
        HttpContext.Current.Response.End();
        HttpContext.Current.Response.Flush();         
    }

The problem is when i see the downloaded doc, it has the remaining page content added with the above <div> content

1 Answer 1

2

I'm missing a Response.Clear at the beginning and Response.End() at the end.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the response. As i'm calling the ExportToWord() using jquery how can i create the doc in this case. please see the edited code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.