Problem in opening the content in HttpWebResponse
-
I have a HttpWebResponse object with the content like text, doc, xls, jpg or other files. and I need to display the content 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 opens with garbage content. What's wrong in this code ?
-
I have a HttpWebResponse object with the content like text, doc, xls, jpg or other files. and I need to display the content 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 opens with garbage content. What's wrong in this code ?
If you want to show a open or save dialog box, try the content-type application/x-force-download
Nirandas, a developer from India. http://www.nirandas.com