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. Download a file from db

Download a file from db

Scheduled Pinned Locked Moved ASP.NET
databasesysadmintoolstutorial
4 Posts 3 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.
  • J Offline
    J Offline
    janani13
    wrote on last edited by
    #1

    hi all, I want to download the file from db which has been save in my application.I downloaded the file . but i want to open it as a pdf file.How to do that.. Here is the code i used to download.

     if (e.CommandName == "ResumeDownload")
        {
    
            Cache\["filename"\] = e.CommandArgument.ToString();
    
            String strRequest = Cache\["filename"\].ToString();// '-- if something was passed to the file querystring
    
            if (strRequest != "")  // 'get absolute path of the file
            {
                String path = Server.MapPath("Attachments\\\\" + strRequest);// 'get file object as FileInfo
    
                FileInfo file = new FileInfo(path);// '-- if the file exists on the server
    
                if (file.Exists)
                {
    
                    Response.Clear();
                    Response.AddHeader("Content-Disposition", "attachment; filename=\\"" + file.Name.ToString()+ "\\"");
                    Response.AddHeader("Content-Length", file.Length.ToString());
                    Response.ContentType = "application/octet-stream";
                    Response.WriteFile(file.FullName);
                    Response.End();// 'if file does not exist
    
                }
                else
                    ClientScript.RegisterStartupScript(typeof(Page), "alert", "<script>alert('File Not Found')</script>");
            }
            else
                Response.Write("Please provide a file to download.");
    
        }
    
    D 1 Reply Last reply
    0
    • J janani13

      hi all, I want to download the file from db which has been save in my application.I downloaded the file . but i want to open it as a pdf file.How to do that.. Here is the code i used to download.

       if (e.CommandName == "ResumeDownload")
          {
      
              Cache\["filename"\] = e.CommandArgument.ToString();
      
              String strRequest = Cache\["filename"\].ToString();// '-- if something was passed to the file querystring
      
              if (strRequest != "")  // 'get absolute path of the file
              {
                  String path = Server.MapPath("Attachments\\\\" + strRequest);// 'get file object as FileInfo
      
                  FileInfo file = new FileInfo(path);// '-- if the file exists on the server
      
                  if (file.Exists)
                  {
      
                      Response.Clear();
                      Response.AddHeader("Content-Disposition", "attachment; filename=\\"" + file.Name.ToString()+ "\\"");
                      Response.AddHeader("Content-Length", file.Length.ToString());
                      Response.ContentType = "application/octet-stream";
                      Response.WriteFile(file.FullName);
                      Response.End();// 'if file does not exist
      
                  }
                  else
                      ClientScript.RegisterStartupScript(typeof(Page), "alert", "<script>alert('File Not Found')</script>");
              }
              else
                  Response.Write("Please provide a file to download.");
      
          }
      
      D Offline
      D Offline
      DoctorMick
      wrote on last edited by
      #2

      Try changing the ContentType to application/pdf. I haven't tried this so there is every chance it won't work but you never know.

      J P 2 Replies Last reply
      0
      • D DoctorMick

        Try changing the ContentType to application/pdf. I haven't tried this so there is every chance it won't work but you never know.

        J Offline
        J Offline
        janani13
        wrote on last edited by
        #3

        ok Mick, let me try.

        1 Reply Last reply
        0
        • D DoctorMick

          Try changing the ContentType to application/pdf. I haven't tried this so there is every chance it won't work but you never know.

          P Offline
          P Offline
          padmanabhan N
          wrote on last edited by
          #4

          DoctorMick wrote:

          application/pdf

          this wont work.. This will open a pdf but without any content.....

          Padmanabhan My Articles: Articles[^] My latest Article: Word Automation[^]

          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