Asp. NET file handling how to operate _ Practical skills

Source: Internet
Author: User

The processing of this piece of file asp.net can be implemented on the drive, file, folder operation.
1, can obtain the information of the drive.

2, create a copy delete move open file.

3. Stream class reading and writing. Read and write text files, read and write binary files, etc.

4, the use of FileUpload control to achieve file upload operation.

Mainly talk about the use of FileUpload control.

Picture file upload.
. aspx file:

<%@ Page language= "C #" autoeventwireup= "true" codefile= "FileUpload.aspx.cs" inherits= "FileUpload"%> <! 
 
DOCTYPE htmlpublic "-//w3c//dtd XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> 
 
 
 

C # code:

The public partialclass FileUpload:System.Web.UI.Page {private string uploaddirectory;//file saves the path string. protected void Page_Load (Objectsender, EventArgs e) {//default to save the file in uploads under the site root file Uploaddirectory = Path.Combine 
 
 (Request.physicalapplicationpath, "uploads"); 
 
 //Upload file. 
 
 protected void Btnfileupload_click (Objectsender, EventArgs e) {//To determine whether a file is submitted. 
 
 if (myfileupload.postedfile.filename== "") {Lblinfo.text = "The file submitted is empty!") Please select the file you want to upload! ";} 
 
  else {//To determine whether the file is larger than 200KB. 
 
  if (myfileupload.postedfile.contentlength> 204800) {lblinfo.text = "file cannot exceed 200KB"; 
 
  else {//Determine the type of file. 
 
  string extension = Path.getextension (myFileUpload.PostedFile.FileName); switch (extension. 
 
   ToLower ()) {case '. bmp ': Case '. gif ': case '. jpg ': break; Default:lblinfo. 
 
   Text = "File type is not *.bmp |*.gif|*.jpg"; 
 
  return; 
 
  //Save the file in the path of our defined uploaddirectory variable in the Web server. FileName remains unchanged. 
 
  String Serverfilename= path.getfilename (myFileUpload.PostedFile.FileName); 
  String fulluploadpath= path.combine (uploaddirectory,serverfilename); 
 
   try {myFileUpload.PostedFile.SaveAs (Fulluploadpath);//File upload. 
 
   Lblinfo.text = "File:" + serverfilename; 
 
   Lblinfo.text + = "successfully uploaded"; 
  Lblinfo.text + = Fulluploadpath; The catch (Exception e2) {Lblinfo.text = e2. 
  message;//upload file error message.  } 
  } 
 } 
 } 
}

Show:

If I "Upload" to you an apple, first of all to judge the "upload" is Apple, and then judge the address of the upload is not, if not your "pocket", do not give, wait until the "upload" success, you give me a note---lable (Apple uploaded successfully into the belly).

Through the final example to you, is not the ASP.net file upload has an image of the understanding, hope that through a series of previous articles to learn to master the ASP.net upload files help Oh.

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.