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. General Programming
  3. C#
  4. How to use Response.AddHeader Method

How to use Response.AddHeader Method

Scheduled Pinned Locked Moved C#
databasehelptutorialquestionsysadmin
6 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.
  • A Offline
    A Offline
    ASPnoob
    wrote on last edited by
    #1

    Hi all, I am trying to change the name of each file as it's being downloaded. The following are what I have done thus far: I have uploaded files to a folder on the web server but replace their names with GUIDs to prevent problems that could arise later on due to duplication . Their original names have been saved to a database and so are their extensions and the paths to their new location on the web server. The problem I am facing now is giving them their original names back when they are downloaded. I know I have to replace the GUID with their original names and original extension when they are being downloaded. I have looked at using Response.AddHeader() method but I am not quite sure on how to make it work. Suppose a file in a folder on the web server have the GUID 123-GtR-XYZ as its name and suppose its original name was test.doc. How do I replace the GUID with the file's original name while it is being downloaded? The following is the method I currently use to retrieve a file from the folder on the web server.

    //Creating a datatable
    DataTable Dtable = new DataTable("INFO");
    //Adding a column of type string name URL
    Dtable.Columns.Add("URL", typeof(string));
    Dtable.Columns.Add("Files", typeof(string));
    //putting files in an array of type string
    string[] fileEntries = Directory.GetFiles(Server.MapPath("~/Uploads/"));
    //int linkL = 0;
    foreach (string fileName in fileEntries)
    {
    Dtable.Rows.Add("~/Uploads/" + Path.GetFileName(fileName));
    //linkL++;
    }

        myGrid.DataSource = Dtable;
        myGrid.DataBind();
    

    Let's say that I have written a SQL query to retrieve the original file name and original extension of the file being downloaded and now we have the GUID, the original file name, and original extension. How do I make use of those data to give each downloaded file it's original name back? Please give a code snippet or point me to a good tutorial if you can. Thank you in advance for your help.

    S P 2 Replies Last reply
    0
    • A ASPnoob

      Hi all, I am trying to change the name of each file as it's being downloaded. The following are what I have done thus far: I have uploaded files to a folder on the web server but replace their names with GUIDs to prevent problems that could arise later on due to duplication . Their original names have been saved to a database and so are their extensions and the paths to their new location on the web server. The problem I am facing now is giving them their original names back when they are downloaded. I know I have to replace the GUID with their original names and original extension when they are being downloaded. I have looked at using Response.AddHeader() method but I am not quite sure on how to make it work. Suppose a file in a folder on the web server have the GUID 123-GtR-XYZ as its name and suppose its original name was test.doc. How do I replace the GUID with the file's original name while it is being downloaded? The following is the method I currently use to retrieve a file from the folder on the web server.

      //Creating a datatable
      DataTable Dtable = new DataTable("INFO");
      //Adding a column of type string name URL
      Dtable.Columns.Add("URL", typeof(string));
      Dtable.Columns.Add("Files", typeof(string));
      //putting files in an array of type string
      string[] fileEntries = Directory.GetFiles(Server.MapPath("~/Uploads/"));
      //int linkL = 0;
      foreach (string fileName in fileEntries)
      {
      Dtable.Rows.Add("~/Uploads/" + Path.GetFileName(fileName));
      //linkL++;
      }

          myGrid.DataSource = Dtable;
          myGrid.DataBind();
      

      Let's say that I have written a SQL query to retrieve the original file name and original extension of the file being downloaded and now we have the GUID, the original file name, and original extension. How do I make use of those data to give each downloaded file it's original name back? Please give a code snippet or point me to a good tutorial if you can. Thank you in advance for your help.

      S Offline
      S Offline
      s_magus
      wrote on last edited by
      #2

      Response.ContentType = this.FileContentType;
      Response.AddHeader("Content-Disposition", "attachment; filename=" + this.FileName);

      If the filename has a space in it then either the whole filename should be quoted (and quotes in the string escaped). Or the entire filename should be encoded, which will use %20 to represent a space.

      A 1 Reply Last reply
      0
      • S s_magus

        Response.ContentType = this.FileContentType;
        Response.AddHeader("Content-Disposition", "attachment; filename=" + this.FileName);

        If the filename has a space in it then either the whole filename should be quoted (and quotes in the string escaped). Or the entire filename should be encoded, which will use %20 to represent a space.

        A Offline
        A Offline
        ASPnoob
        wrote on last edited by
        #3

        Hi s_magus, thanks for replying. How do I make use of the two lines you've posted in my existing code? Thanks again for replying.

        1 Reply Last reply
        0
        • A ASPnoob

          Hi all, I am trying to change the name of each file as it's being downloaded. The following are what I have done thus far: I have uploaded files to a folder on the web server but replace their names with GUIDs to prevent problems that could arise later on due to duplication . Their original names have been saved to a database and so are their extensions and the paths to their new location on the web server. The problem I am facing now is giving them their original names back when they are downloaded. I know I have to replace the GUID with their original names and original extension when they are being downloaded. I have looked at using Response.AddHeader() method but I am not quite sure on how to make it work. Suppose a file in a folder on the web server have the GUID 123-GtR-XYZ as its name and suppose its original name was test.doc. How do I replace the GUID with the file's original name while it is being downloaded? The following is the method I currently use to retrieve a file from the folder on the web server.

          //Creating a datatable
          DataTable Dtable = new DataTable("INFO");
          //Adding a column of type string name URL
          Dtable.Columns.Add("URL", typeof(string));
          Dtable.Columns.Add("Files", typeof(string));
          //putting files in an array of type string
          string[] fileEntries = Directory.GetFiles(Server.MapPath("~/Uploads/"));
          //int linkL = 0;
          foreach (string fileName in fileEntries)
          {
          Dtable.Rows.Add("~/Uploads/" + Path.GetFileName(fileName));
          //linkL++;
          }

              myGrid.DataSource = Dtable;
              myGrid.DataBind();
          

          Let's say that I have written a SQL query to retrieve the original file name and original extension of the file being downloaded and now we have the GUID, the original file name, and original extension. How do I make use of those data to give each downloaded file it's original name back? Please give a code snippet or point me to a good tutorial if you can. Thank you in advance for your help.

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          What, exactly, do you have against using the ASP.NET forum, which is where your posts belong?

          *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

          "Mind bleach! Send me mind bleach!" - Nagy Vilmos

          CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

          A 1 Reply Last reply
          0
          • P Pete OHanlon

            What, exactly, do you have against using the ASP.NET forum, which is where your posts belong?

            *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

            "Mind bleach! Send me mind bleach!" - Nagy Vilmos

            CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

            A Offline
            A Offline
            ASPnoob
            wrote on last edited by
            #5

            Nothing, since my question had to do with c# I just thought it was ok to post here.

            P 1 Reply Last reply
            0
            • A ASPnoob

              Nothing, since my question had to do with c# I just thought it was ok to post here.

              P Offline
              P Offline
              Pete OHanlon
              wrote on last edited by
              #6

              It doesn't matter what the language is, the problem is an ASP.NET one. So far, all your questions should have been asked there.

              *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

              "Mind bleach! Send me mind bleach!" - Nagy Vilmos

              CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

              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