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. Parsing a Path to the Server

Parsing a Path to the Server

Scheduled Pinned Locked Moved ASP.NET
htmlcsssysadminjsonhelp
5 Posts 4 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

    Hello all I had posted this question in the wrong forum and was asked to move it here. I'm trying to download a file from a folder on the server but I don't know how to parse the path inside of the HREF attribute of the anchor tage. I'm trying to modify the following code to be used with HTML anchor tag:

    protected void Page_PreRender(object sender, EventArgs e)
    {
    DataTable dt = new DataTable("INFO");
    dt.Columns.Add("url", typeof(string));

      string\[\] fileEntries = Directory.GetFiles(Server.MapPath("~/Documents/"));      
     
      dt.Rows.Add("~/Documents/" + Path.GetFileName(fileName));      
    
      grid.DataSource = dt;
      grid.DataBind();
    }
    

    I wanted to do something like the following on my Page.aspx file:

    DOWNLOAD

    However, the above cannot be done. Please help me find and alternative, thanks in advance for your help.

    H 1 Reply Last reply
    0
    • A ASPnoob

      Hello all I had posted this question in the wrong forum and was asked to move it here. I'm trying to download a file from a folder on the server but I don't know how to parse the path inside of the HREF attribute of the anchor tage. I'm trying to modify the following code to be used with HTML anchor tag:

      protected void Page_PreRender(object sender, EventArgs e)
      {
      DataTable dt = new DataTable("INFO");
      dt.Columns.Add("url", typeof(string));

        string\[\] fileEntries = Directory.GetFiles(Server.MapPath("~/Documents/"));      
       
        dt.Rows.Add("~/Documents/" + Path.GetFileName(fileName));      
      
        grid.DataSource = dt;
        grid.DataBind();
      }
      

      I wanted to do something like the following on my Page.aspx file:

      DOWNLOAD

      However, the above cannot be done. Please help me find and alternative, thanks in advance for your help.

      H Offline
      H Offline
      Hum Dum
      wrote on last edited by
      #2

      Modify it like

      protected void Page_PreRender(object sender, EventArgs e)
      {
      DataTable dt = new DataTable("INFO");
      dt.Columns.Add("url", typeof(string));

          string\[\] fileEntries = Directory.GetFiles(Server.MapPath("~/Documents/"));
      
          foreach (string f in fileEntries)
          {
              DataRow dr = dt.NewRow();
              dr\["url"\] = " [DOWNLOAD](~/Documents/" + Path.GetFileName\(f\) + ") ";
              dt.Rows.Add(dr);
          }        
          grid.DataSource = dt;
          grid.DataBind();
      }
      
      P 1 Reply Last reply
      0
      • H Hum Dum

        Modify it like

        protected void Page_PreRender(object sender, EventArgs e)
        {
        DataTable dt = new DataTable("INFO");
        dt.Columns.Add("url", typeof(string));

            string\[\] fileEntries = Directory.GetFiles(Server.MapPath("~/Documents/"));
        
            foreach (string f in fileEntries)
            {
                DataRow dr = dt.NewRow();
                dr\["url"\] = " [DOWNLOAD](~/Documents/" + Path.GetFileName\(f\) + ") ";
                dt.Rows.Add(dr);
            }        
            grid.DataSource = dt;
            grid.DataBind();
        }
        
        P Offline
        P Offline
        Pete OHanlon
        wrote on last edited by
        #3

        Hum Dum wrote:

        dr["url"] = "<a href='~/Documents/" + Path.GetFileName(f) + "'> DOWNLOAD </a>";

        That just posts a string as the anchor tag, and doesn't resolve the ~. That's the bit he needs help with.

        *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

        S 1 Reply Last reply
        0
        • P Pete OHanlon

          Hum Dum wrote:

          dr["url"] = "<a href='~/Documents/" + Path.GetFileName(f) + "'> DOWNLOAD </a>";

          That just posts a string as the anchor tag, and doesn't resolve the ~. That's the bit he needs help with.

          *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

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

          A couple of ways to resolve urls. Virtual Path Utility[^] http://refactoringaspnet.blogspot.com/2009/09/different-approaches-for-resolving-urls.html[^]

          P 1 Reply Last reply
          0
          • S s_magus

            A couple of ways to resolve urls. Virtual Path Utility[^] http://refactoringaspnet.blogspot.com/2009/09/different-approaches-for-resolving-urls.html[^]

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

            Try replying to the OP, rather than me. He won't get emailed about answers to me.

            *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