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.