How to handle ASP. NET files? asp.net File Processing
ASP. NET can be used to process files on drives, files, and folders.
1. You can obtain the drive information.
2. Create, copy, delete, and move an opened file.
3. Stream-type read/write. Read/write text files and binary files.
4. Use the FileUpload control to upload files.
This section describes how to use the FileUpload control.
Upload an image file.
. 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:
Public partialclass FileUpload: System. web. UI. page {private string uploadDirectory; // file storage path string. protected void Page_Load (objectsender, EventArgs e) {// by default, the file is saved in Uploads under the root file of the site. uploadDirectory = Path. combine (Request. physicalApplicationPath, "Uploads");} // upload a file. protected void btnFileUpload_Click (objectsender, EventArgs e) {// determine whether a file is submitted. if (myFileUpload. postedFile. fileName = "") {lblInfo. text =" The submitted file is empty! Select the file to upload! ";} Else {// determine whether the file size exceeds kb. if (myFileUpload. postedFile. contentLength & gt; 204800) {lblInfo. text = "file cannot exceed kb";} else {// determine the file type. string extension = Path. getExtension (myFileUpload. postedFile. fileName); switch (extension. toLower () {case ". bmp ": case ". gif ": case ". jpg ": break; default: lblInfo. text = "the file type is not *. bmp | *. gif | *. jpg "; return;} // save the file to the path of the uploadDirectory variable defined by the web server. // The file name 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 to"; lblInfo. text + = fullUploadPath;} catch (Exception e2) {lblInfo. text = e2.Message; // File Upload error message .}}}}}
Display:
If I "Upload" an apple to you, I first need to judge that "Upload" is not an apple, and then determine whether the upload address is correct. If it is not your "pocket", I will not give it, when "Upload" is successful, you can give me another note --- Lable (Apple upload is successful ).
Finally, I will give you an example to see if it is applicable to ASP. NET File Upload has an image understanding, hope to learn through a series of previous articles for everyone to master ASP.. NET.