In directory browsing, if you select a file, click on the "Open" button to download the file. Of course, you can also choose to open directly, as shown in Figure 1.
Figure 1 Download and online open Interface
Click the Open button to open the file directly, and click the Save button to download the file.
Code implementation
Open the file by clicking on the Open button, so make a decision in the Open event to see whether the folder or file is selected. Therefore, the Btnopen_click () event has been modified, the following aggravating section for the new code:
Copy Code code as follows:
private void Btnopen_click (object sender, System.EventArgs e)
{
if (filelist.selecteditem.text== "Back to Parent Directory")//Return to Superior directory
{
String Parentpath=directory.getparent (Currentpath). ToString ();
Loaddir (Parentpath);
Return
}
else if (FileList.SelectedItem.Text.IndexOf (".") >0)//Open File
{
FileDownload (FileList.SelectedItem.Text);
}
else//Open Directory
{
Loaddir (FileList.SelectedItem.Text);
}
}
If you select a file, the FileDownload () method is called, and the Code section is as follows:
Copy Code code as follows:
private void FileDownload (string fullfilename)
{
FileInfo DownloadFile = new FileInfo (yourfilename); Set up files to download
Response.Clear (); Clears all content output from the buffer stream
Response.ClearHeaders (); Clears all headers in the buffer stream
Response.Buffer = false; Set buffer output to False
Sets the HTTP MIME type of the output stream to Application/octet-stream
Response.ContentType = "Application/octet-stream";
To add an HTTP header to an output stream
Response.appendheader ("Content-disposition", "attachment;filename=" + httputility.urlencode (DownloadFile.FullName , System.Text.Encoding.UTF8));
Response.appendheader ("Content-length", DownloadFile.Length.ToString ());
Writes the specified file directly to the HTTP content output stream.
Response.WriteFile (Downloadfile.fullname);
Response.Flush (); Sends all current buffered output to the client
Response.End (); Sends all current buffered output to the client
}
This code is to use the form of output stream file transfer, it is difficult to understand. It can be difficult to add a hyperlink control to a page if you are dealing with it in a different way. When the appropriate file is selected, it is linked to the appropriate path. No more talking here.