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 ?
No one has replied yet! Why not be the first?
Sign in or Join us (it's free).