Displaying Files with a Hyperlink to download them
page 2 of 2
by Rajiv R.
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 26553/ 80
Article Contents:


We start with creating a DirectoryInfo object and the directory whose files has been to displayed is passed to the constructor.

System.IO.FileInfo[] fiArr=di.GetFiles();

Next, we call the GetFiles() method of the DirectoryInfo object. It returns a array of FileInfo objects.

foreach (System.IO.FileInfo fi in fiArr)
    Response.Write("<a href='"+fi.Name+"'>"+fi.Name+ "<br>") ;

Last, for each FileInfo object, we construct a anchor tag.

Alternatively, We can create Hyperlink objects,set the Text and NavigateURL properties and add it to the page.

HyperLink hl=new HyperLink(); hl.NavigateUrl=fi.Name; hl.Text=fi.Name; Page.Controls.Add(hl);

VB.NET Code:

Dim di As New System.IO.DirectoryInfo("C:\Inetpub\wwwroot\WebApplication2\")
Dim fiArr As System.IO.FileInfo() = di.GetFiles()
Dim fi As System.IO.FileInfo

For Each fi In fiArr
    Response.Write("<a href='" + fi.Name + "'>" + fi.Name + "<br>")
Next fi

Send your comments to rajiv.raj@wipro.com

View Entire Article

User Comments

Title: to download file how to set path on hyperlink   
Name: bp sharma
Date: 2011-05-02 6:28:21 AM
i want download file from database using hyperlink in gridview
how i set path in "NavigateUrl" property please give complete solution thanxxx
Title: Nice Article .....   
Name: Shailesh
Date: 2010-06-18 6:24:07 AM
It is really a nice article ,with small and useful code
Title: fine   
Name: really
Date: 2009-01-19 5:50:45 AM
workrd fo me
Title: download dialogbox print button   
Name: Rashda
Date: 2008-10-17 6:01:48 AM
plz tell me how to remove print button from the file download dialog box
Title: huh???   
Name: AW
Date: 2008-06-17 3:07:04 PM
I don't get it? there's code all over the page. Please organize ur code so I can understand what ur doing..????
Title: more details   
Name: krithi
Date: 2006-07-25 1:08:42 AM
Need more details to download files. This code above just helps to navigate to the URL.
Title: Not clear   
Name: Lucky
Date: 2006-06-23 2:23:03 AM
Topics are good. But solutionsa re not clear.PLz make them clear.
Title: failed   
Name: Danny
Date: 2006-05-29 6:57:29 AM
This code doesnt helps to download files.
When I use this code in ASP.NET (VB), I navigate to corresponding link. I cant even do right-click/save target as option
Title: Help   
Name: vino
Date: 2006-02-16 4:13:43 AM
how to set a hyper link for a file which contains list information
Title: Help   
Name: Ram Konara
Date: 2005-05-19 8:28:52 PM
I need to Map to a shared drive on a remote Server using a ID and Password from ASP.Net. Get the File Information from the shared Drive and Disconnect the Shared drive. Please help me
Title: not complete   
Name: shu
Date: 2005-05-12 4:57:55 AM
i wanna know how to actually download the files
Title: Poor grammer, but simple and to the point.   
Name: Brunhilde
Date: 2004-11-03 11:37:19 PM
I'd like to see more info about how the files are downloaded though.

Product Spotlight
Product Spotlight 

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2023 ASPAlliance.com  |  Page Processed at 2023-09-29 6:14:01 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search