ASP. NET upload files

Source: Internet
Author: User

If you have nothing to worry about today, you can encapsulate the previously written Method for uploading files. It feels very useful after encapsulation. If you have any errors, please give us some guidance, the following code first creates a class tool. The following is a specific encapsulation method: /// <summary> /// upload a file /// </summary> /// <param name = "fileUploadControl"> upload a file Control </param> /// <param name = "path"> the relative path of the uploaded server is as follows: "~ \ Resource \ Images "</param> /// <param name =" arrayType "> file format </param> /// <param name =" errorMessage "> return error message </param> /// <param name =" filePath "> return absolute path </param> /// <returns> return value </returns> public static bool UploadFile (FileUpload fileUploadControl, string path, string [] arrayType, out string errorMessage, out string filePath) {if (fileUploadControl. hasFile) {string fullFileName = fileUploadContro L. postedFile. fileName; string fileName = fullFileName. substring (fullFileName. lastIndexOf ("\") + 1); string fileType = fileName. substring (fileName. lastIndexOf ('. ') + 1); if (arrayType. contains (fileType. toLower () {string basePath = HttpContext. current. server. mapPath (path); string subPath = basePath + "\" + DateTime. now. toString ("yyyyMMddhhmmss") + fileName; // here is the time + name of the file name for the upload file if (! File. Exists (subPath) {if (! Directory. exists (basePath) Directory. createDirectory (basePath); filePath = subPath; errorMessage = string. empty; return true;} else {errorMessage = "the file you uploaded already exists. Please rename it and upload it again! "; FilePath = string. Empty; return false ;}} else {errorMessage =" the file you uploaded is incorrectly formatted! "; FilePath = string. Empty; return false ;}} else {errorMessage =" You have not uploaded a file or the uploaded file is Empty! "; FilePath = string. empty; return false ;}}the following is the usage method (Click Event of the upload button): protected void btnupload_Click (object sender, EventArgs e) {string errorMessage = string. empty; string filePath = string. empty; string [] arrayType = new string [] {"jpg", "png", "jpeg", "bmp", "gif"}; string path = "~ \ Resource \ Images "; bool status = tool. uploadFile (this. fileUpload1, path, arrayType, out errorMessage, out filePath); if (status) {try {FileUpload1.PostedFile. saveAs (filePath); ClientScript. registerStartupScript (GetType (), "alert", "alert ('upload successful! ') ", True);} catch (Exception) {ClientScript. RegisterStartupScript (GetType ()," alert "," alert (' Upload Failed! ') ", True); throw ;}} else {ClientScript. registerStartupScript (this. getType (), "alert", "alert ('" + errorMessage + "');", true) ;} OK, okay, so that you can implement the general upload function.

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.