In directory browsing, if you select a file, click the OPEN button to download the file. Of course, you can also choose to open it directly, as shown in 1.
Figure 1 download and open online
Click Open to open the file. Click Save to download the file.
Code Implementation
To open the file, click the "open" button. Therefore, you must check whether the selected folder or file is selected in the "open" event. Therefore, the btnOpen_Click () event is modified. The additional code is as follows:Copy codeThe Code is as follows: private void BtnOpen_Click (object sender, System. EventArgs e)
{
If (FileList. SelectedItem. Text = "back to parent directory") // return to parent directory
{
String ParentPath = Directory. GetParent (CurrentPath). ToString ();
LoadDir (ParentPath );
Return;
}
Else if (FileList. SelectedItem. Text. IndexOf (".")> 0) // open the file
{
FileDownload (FileList. SelectedItem. Text );
}
Else // open the Directory
{
LoadDir (FileList. SelectedItem. Text );
}
}
If you select a file, call the FileDownload () method. The Code section is as follows:Copy codeThe Code is as follows: private void FileDownload (string FullFileName)
{
FileInfo DownloadFile = new FileInfo (YourFileName); // you can specify the object to be downloaded.
Response. Clear (); // Clear all content output in the buffer stream
Response. ClearHeaders (); // clear all headers in the buffer stream
Response. Buffer = false; // set the Buffer output to false.
// Set the http mime type of the output stream to application/octet-stream
Response. ContentType = "application/octet-stream ";
// Add the HTTP header to the output stream
Response. AppendHeader ("Content-Disposition", "attachment; filename =" + HttpUtility. UrlEncode (DownloadFile. FullName, System. Text. Encoding. UTF8 ));
Response. AppendHeader ("Content-Length", DownloadFile. Length. ToString ());
// Write the specified file directly to the HTTP Content output stream.
Response. WriteFile (DownloadFile. FullName );
Response. Flush (); // send the Current Buffer output to the client
Response. End (); // send all current buffered output to the client
}
This code is used to transmit files in the form of output streams, which is difficult to understand. If you use another method for processing, it is difficult to add a HyperLink control on the page. When you select a file, link it to the corresponding path. I will not talk about it here.