Asp. NET Design network hard drive download or online View implementation code _ Practical skills

Source: Internet
Author: User
Tags parent directory
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.
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.