locked
A coding problem with Response Class RRS feed

  • Question

  • User137033276 posted

    Scenario:        

    I have implemented a function that when user click a button, he can download the a ms word file dynamically generated. The implementation is summerized as follows:

            Response.AddHeader("Content-Disposition", "filename=Referral.doc");  // Specify the file name
            Response.ContentType = "application/msword";  // Set content type

            // Prepare for content of the file
            string content = "" ;
            content += ...
            ...
            ...

            // Write to client
            byte[] bytes = System.Text.Encoding.GetEncoding("big5").GetBytes(readfile);
            Response.BinaryWrite(bytes);
            Response.End();   

    Problem:

    The above code snippet work well. However, if  I replace the italic code section with the following:

            Response.Charset = "Big5";
            Response.Write(content);
            Response.End();   

    Then, MS Word can not properly open the responsed file (Referral.doc) as if it is an empty file although it indeed contains string of content variable (if opened with UltraEditor, the content is properly displayed).

    I believe this is caused by the coding problem. Why Response.Write(content) can not perperly work even if I have speicfy Response.Charset = "Big5". Dosen't the Charset property explicitly set the encoding mode of Response.Write? Or, it just add a http header to the response but does nothing about encoding.

    Thanks in advance for reading thoroughly the long long question.

    Best Wishes,

    Ricky.

    Monday, April 2, 2007 6:31 AM

Answers

  • User113421904 posted

    Hi,

    Word document is binary format, so you can only use BinaryWrite method, but can't send it as a string. I don't think this is the problem of Charset or Encoding, I think you can try GB2312 instead to see if this is the problem.

     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 5, 2007 11:20 PM

All replies

  • User137033276 posted
    By the way, the content variable is constructed by reading a .htm template and replacing tokens defned, such as <Title>, with users inputs in TextBoxs. Since users are Chinses, the inputs contain Big5 charset inevitably.
    Monday, April 2, 2007 10:59 AM
  • User113421904 posted

    Hi,

    Word document is binary format, so you can only use BinaryWrite method, but can't send it as a string. I don't think this is the problem of Charset or Encoding, I think you can try GB2312 instead to see if this is the problem.

     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 5, 2007 11:20 PM
  • User137033276 posted

    Thanks a lot, Zhao.

    You give me another direction to troubleshoot. But Word indeed can read .txt files, doesn't it? 

    Regards,

    Ricky

    Sunday, April 8, 2007 8:51 PM
  • User113421904 posted

    Hi aliasncnu,

    Yes, Word can read .txt files. In ASP.NET, you can use BinaryWrite() for both .doc and .txt files, however only .txt file can be load into string and Write() method is used to output the string.

     

    Monday, April 9, 2007 11:58 PM
  • User137033276 posted

    Thanks for your kindly response at first.

    The content variable (refer to the original post) is indeed populated wtih a .htm template where the special tokens such as [Title] is replaced with user input (as in TextBoxes). Following is the code sinppet:

           -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

            FileStream fs;
            StreamReader sr;

            fs = System.IO.File.OpenRead(Server.MapPath(WebConfigurationManager.AppSettings["Referral_OrgTemplatePath"]));  // This will map to a temple.htm
            sr = new StreamReader(fs, System.Text.Encoding.GetEncoding("big5"));

            string content = sr.ReadToEnd();

            content = content .Replace("[txt_tr_hnamec]", O_HNamec.Text);
            .............................................................................................
            .............................................................................................

            Response.Charset = "Big5";
            Response.Write(readfile);
            Response.End();

           -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    Best Regards,

    Ricky

    Tuesday, April 10, 2007 12:39 AM