Problem in opening the content in HttpWebResponse

  • 9 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.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);

    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).


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.

“Theory is when you know something, but it doesn't work. Practice is when something works, but you don't know why. Programmers combine theory and practice: Nothing works and they don't know why.”