Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Problem in opening the content in HttpWebResponse

Problem in opening the content in HttpWebResponse

Scheduled Pinned Locked Moved ASP.NET
helpquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    smsayami
    wrote on last edited by
    #1

    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 ?

    N 1 Reply Last reply
    0
    • S smsayami

      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 ?

      N Offline
      N Offline
      Nirandas
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups