The example in this article describes the method by which ASP.net gets the server-specified folder directory file and provides downloads. Share to everyone for your reference. The implementation methods are as follows:
Copy Code code as follows:
String dirpath = HttpContext.Current.Server.MapPath ("uploads/");
if (directory.exists (Dirpath))
{
Get directory information
DirectoryInfo dir = new DirectoryInfo (Dirpath);
Get a list of catalog files
fileinfo[] files = dir. GetFiles ("*.*");
string[] FileNames = new String[files. Length];
Temporary data tables
DataTable dt = new DataTable ();
Dt. Columns.Add ("FileName");
foreach (FileInfo FileInfo in Files)
{
DataRow dr = dt. NewRow ();
dr["FileName"] = Fileinfo.name;
Dt. Rows.Add (DR);
}
Repeater1.datasource = DT;
Repeater1.databind ();
}
if (E.commandname = "Down")
{
Try
{
String downloadfilename = "~/uploads/" + e.commandargument.tostring ();//file path
string filepath = Server.MapPath (downloadfilename);
string filename = Path.getfilename (filepath);
FileInfo file = new FileInfo (filepath);
Response.Clear ();
Response.ContentType = "Application/octet-stream";
Response.AddHeader ("Content-disposition", "attachment; Filename= "+ httputility.urlencode (filename, System.Text.Encoding.UTF8));
Response.AddHeader ("Content-length", file. Length.tostring ());
Response.Flush ();
Response.WriteFile (filepath);
}
Catch
{
Response.Write ("<script>alert (' no source file found ') </script>");
}
}
I hope this article will help you with the ASP.net program design.