How to handle ASP. NET files? asp.net File Processing

Source: Internet
Author: User

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.

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.