Asp.net File Download Processing
Recently, a project requires BS to download files. I used to remember to execute the file directly through the fileopendialog or filesave control when saving the file in C, however, BS does not seem to have a ready-made control to select the control for the download file path, and then finds some relevant information online. There are roughly two download methods.
1. Download the client file and path directly. The Code is as follows:
Try
{
String filename = "file name ";
String filepath = server. mappath ("/"); // path
Fileinfo = new fileinfo (filepath );
If (fileinfo. exists)
{
Response. Clear ();
Response. clearcontent ();
Response. clearheaders ();
Response. addheader ("content-disposition", "attachment; filename =" + filename );
Response. addheader ("Content-Length", fileinfo. length. tostring ());
Response. addheader ("content-transfer-encoding", "binary ");
Response. contenttype = "application/octet-stream ";
Response. contentencoding = system. Text. encoding. getencoding ("UTF-8 ");
Response. writefile (fileinfo. fullname );
Response. Flush ();
Response. End ();
}
Else
{
Alert. Show ("the downloaded file does not exist. Please confirm ");
}
}
Catch
{
Alert. Show ("Download failed ");
}
2. You can directly call <a href to download the file as follows:
Create a label control. When you click the label control, it is displayed on the download link page.
This. label1.text = "<a href =/" + path for File Download + "target = _ blank> download </a> ";
When you click label, link to a specific file for download.