ASP. NET File Upload class is simple and easy to use, asp.net File Upload class

Source: Internet
Author: User

ASP. NET File Upload class is simple and easy to use, asp.net File Upload class

Call:

UploadFile uf = new UploadFile (); // parameter setting // uf. setFilePath = "" sets the Save path. The default value is upload // uf. setFileType = ". exe "sets the allowed suffix, Which is .htm, .xls,.xlsx,.doc,.docx,.txt // uf. setMaxSizeM = 100 set the maximum upload size. The default value is 10 MB. // run the command to save uf. save ("file"/* name of input file */, true); // true will automatically process files of the same name, and false will overwrite files of the same name // return information var isError = uf. getError; // determine whether the segment is uploaded successfully. var webPath = uf. getWebFilePath; // return the web path var response = uf. getMessage; // return the upload information var filePath = uf. getFilePath; // reverse file path

 

 

 

Code:

 

Using System; using System. collections. generic; using System. IO; using System. linq; using System. security. cryptography; using System. web; namespace SyntacticSugar {// <summary> // ** Description: single-file upload class (Multi-File Upload is not supported currently) // ** Creation Time: // ** modification time:-// Author: sunkaixuan // </summary> public class UploadFile {// <summary> // file storage path // </summary> public string SetFileDirectory {get; set ;} /// <summary> // /The types of files that can be uploaded. Separate them with commas (,). All files must be in lower case! ///// Format: ".gif, .exe" or more // </summary> public string SetFileType {get; set ;} /// <summary> /// the size of the file to be uploaded (unit: M) /// </summary> public double SetMaxSizeM {get; set ;} /// <summary> // upload error // </summary> public bool GetError {get; private set ;} /// <summary> /// message /// </summary> public string GetMessage {get; private set ;} /// <summary> /// file path /// </summary> public string GetFilePath {Get; private set ;}//< summary> /// website path /// </summary> public string GetWebFilePath {get; private set ;} /// <summary> /// get the file name /// </summary> public string GetFileName {get; private set;} public UploadFile () {SetFileDirectory = "/upload "; setFileType = ".,.xls,.xlsx,.doc,.docx,.txt,.png,.jpg,.gif"; SetMaxSizeM = 10 ;} /// <summary> /// single file upload class /// </summary> /// <param name = "directory"> File storage path </param> /// <param name = "fileType"> supported file types </param> /// <param name = "maxSizeM"> allow upload size (unit: m) </param> public UploadFile (string directory, string fileType, double maxSizeM) {SetFileDirectory = directory; SetFileType = fileType; SetMaxSizeM = maxSizeM ;} /// <summary> /// Save the Form file, based on file name /// </summary> /// <param name = "formField"> </param> /// <param name = "isRenameSameFile"> the value is true, with the same name. file Rename, false overwrites the original file </param> /// <param name = "fileName"> New file name </param> /// <returns> </returns> public string Save (string formField, bool isRenameSameFile, string fileName = null) {var Response = HttpContext. current. response; var Request = HttpContext. current. request; // obtain the uploaded file HttpFileCollection file = Request. files; HttpPostedFile postFile = file [formField]; return Save (postFile, isRenameSameFile, fi LeName);} // <summary> // Save the Form file, based on HttpPostedFile /// </summary> /// <param name = "postFile"> HttpPostedFile </param> /// <param name = "isRenameSameFile"> the value is true, with the same name. rename the file, false overwrites the original file </param> /// <param name = "fileName"> New file name </param> /// <returns> </returns> public string Save (httpPostedFile postFile, bool isRenameSameFile, string fileName = null) {try {// file name fileName = string. isNullOrEmpty (fileName)? PostFile. fileName: fileName; // verify the format of this. checkingType (postFile. fileName); // verify the size of this. checkSize (postFile); if (GetError) return string. empty; string webDir = string. empty; // obtain the storage directory var directory = this. getDirectory (ref webDir); var filePath = directory + fileName; if (System. IO. file. exists (filePath) {if (isRenameSameFile) {filePath = directory + DateTime. now. toString ("yyyyMMddhhssms") + "-" + FileName;} else {System. IO. file. delete (filePath) ;}// save the file postFile. saveAs (filePath); GetFilePath = filePath; GetWebFilePath = webDir + fileName; GetFileName = postFile. fileName; return filePath;} catch (Exception ex) {TryError (ex. message); return string. empty ;}} private void CheckSize (HttpPostedFile PostFile) {if (PostFile. contentLength/1024.0/1024.0> SetMaxSizeM) {TryError (strin G. Format ("sorry, the uploaded file is too large and cannot exceed {0} M! ", SetMaxSizeM ));}} /// <summary> /// obtain the directory /// </summary> /// <returns> </returns> private string GetDirectory (ref string webDir) {// storage directory string directory = this. setFileDirectory; // directory Format string Date = DateTime. now. toString ("yyyy-MM/dd"); webDir = directory + "/" + Date + '/'; string dir = HttpContext. current. server. mapPath (webDir); // create the Directory if (Directory. exists (dir) = false) Directory. createDir Ecloud (dir); return dir ;} /// <summary> /// verify the file type /// </summary> /// <param name = "fileName"> </param> private void CheckingType (string fileName) {// obtain the list of allowed upload types string [] typeList = this. setFileType. split (','); // obtain the upload file type (lower case) string type = Path. getExtension (fileName ). toLowerInvariant (); // verification type if (typeList. contains (type) = false) this. tryError ("the file type is invalid! ");} /// <Summary> /// throw an error // </summary> /// <param name = "Msg"> </param> private void TryError (string msg) {this. getError = true; this. getMessage = msg ;}}}

  

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.