ASP. NET design network hard disk Upload files

Source: Internet
Author: User
Tags file upload split webfile
In the end, users need to deal with files. Folders are only used for convenient management. Uploading and downloading files has become an important part of the "network hard disk" function design.
1. Interface layout
These functions involve two controls: one is the File Field control (ID) for WebFile, which is used to browse the Upload File path; the other is the upload button (ID) for btnUpLoad, click it to complete file upload.
2. Code implementation
On the design panel, double-click the btnUpLoad button to add Event Processing for it. The code is as follows:
Private void BtnUpload_Click (object sender, System. EventArgs e)
{
If (WebFile. PostedFile. FileName = "")
{
Info. Text = "select the file to be uploaded first ";
Return;
}
Try
{
Char [] spliter = {''};
String [] FileName = WebFile. PostedFile. FileName. Split (spliter, 10 );
String FullPath = CurrentPath + @ "" + FileName [FileName. Length-1];
// Generate the complete file name
WebFile. PostedFile. SaveAs (FullPath); // Save the file
LoadDir (CurrentPath); // reload the current directory
}
Catch
{
Info. Text = "failed to upload the file. Please contact the administrator ";
}
}
First, you must determine whether you have selected the file to be uploaded. This can be obtained through the WebFile. PostedFile. FileName attribute. It should be noted that WebFile is a File Field control object, which belongs to the HtmlInputFile class. The HtmlInputFile. PostedFile method is used to obtain access to the uploaded files on the client.
After you confirm that you have selected the file to be uploaded, you must set the upload path. First, obtain the file name. Because the user provides a complete client path, you need to use the Split () method to Split by the "//" separator, and the results are saved in an array. For example, "C: // UserDir // Chapter1 // Ch1.doc" can be divided into four parts. The last part (the last part of the array) is the name of the desired file. In the above program, FileName [FileName. Length-1] is the file name uploaded by the user.
After obtaining the upload path, you can upload the file. The PostedFile. SaveAs () method is used here. After the upload is complete, call LoadDir () to display the uploaded file name.
Click the Browse button during running. The dialog box shown in figure 16-5 is displayed. After selecting a file, click OK to complete file upload.
Figure "browse" the file to be uploaded
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.