asp.net get the server to specify folder directory files and provide a download method _ Practical Tips

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.