Problem in opening the content in HttpWebResponse

  • 12 years ago

    I have a HttpWebResponse object with the content like text, doc, xls, jpg or other files. and I need to display the content in open or save way. For that I wrote the code like this:

    public void DisplayDocument(HttpWebResponse docResponse, string name)
            {
               
                  StreamReader reader = new StreamReader(docResponse.GetResponseStream());
                  string tmpContent = reader.ReadToEnd();
                  System.Web.HttpContext.Current.Response.ContentType = docResponse.ContentType;
                  System.Web.HttpContext.Current.Response.ClearContent();
                  System.Web.HttpContext.Current.Response.ContentType = docResponse.ContentType;
                  System.Web.HttpContext.Current.Response.AddHeader("Content-Length", docResponse.ContentLength.ToString());
                  System.Web.HttpContext.Current.Response.AppendHeader("content-disposition", "attachment;filename=" + Name);
                  System.Web.HttpContext.Current.Response.Write(tmpContent);
                  docResponse.Close();
                  HttpContext.Current.ApplicationInstance.CompleteRequest();                        
         }

    If the HttpResponse has text file content, I can open or save. But if it has some other files like image, doc, excel content, they either don't open or open with garbage content. What's wrong in this code ?

Post a reply

No one has replied yet! Why not be the first?

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“The greatest performance improvement of all is when a system goes from not-working to working.” - John Ousterhout