Call:
UploadFile UF =NewUploadFile (); //parameter Settings//uf. Setfilepath= "" Setting save path, default is upload//uf. Setfiletype= ". exe" to set the allowed suffix format, by default. Pdf,.xls,.xlsx,.doc,.docx,.txt//uf. setmaxsizem=100 set maximum upload size default 10M//Execute Saveuf. Save ("file" /*the name of the input file*/,true);//true to automatically process files with the same name, false overwrites files with the same name//return information varIsError = uf. GetError;//whether the sentence was successfully uploaded varWebpath = uf. Getwebfilepath;//back to Web path varResponse = uf. GetMessage;//back to upload information varFilePath = 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 (temporarily does not support multi-file uploads//* * Founding Date: 2015-5-27) * * Modification Time:-///* * Sunkaixuan//</summary> public class UploadFile {//<summary> File save path//</summary> public string Setfiledirectory {get; set;} <summary>///allow upload of file types, comma split, must all lowercase! Format: ". Gif,.exe" or more//</summary> public string Setfiletype{get;set;} <summary>///allow upload of how much size (in M)////</summary> public double Setmaxsizem {get;set;} <summary>///Upload error///</summary> public bool GetError {get; private set;} <summary>///News///</summary> public string GetMessage {get; private set;}///<summary>///file path///</summary> public string GetFilePath {get; private s Et }///<summary>//website Path///</summary> public string Getwebfilepath {get; privat e set; }///<summary>//Get file name///</summary> public string GetFileName {get; private Set } public UploadFile () {setfiledirectory = "/upload"; Setfiletype = ". Pdf,.xls,.xlsx,.doc,.docx,.txt"; Setmaxsizem = 10; }///<summary>///Single File upload class///</summary>//<param name= "directory" > File save path </param>//<param name= "FileType" > Allowed file Types </param>//<param name= "Maxsizem" > Allow on Number of sizes (units: M) </param> public uploadfile (string directory, string filetype,double maxsizem) { Setfiledirectory =directory; Setfiletype = FileType; SeTmaxsizem = Maxsizem; }///<summary>///Save form File///</summary>//<param name= "FormField" ></p aram>//<param name= "Isrenamesamefile" > value is true rename file with same name, false overwrite original file </param>//<param N Ame= "filename" > New filename </param>//<returns></returns> public string Save (string Formfiel D, bool isrenamesamefile, string fileName = null) {try {var Response = Httpc Ontext. Current.response; var Request = HttpContext.Current.Request; Get uploaded files Httpfilecollection file = Request.Files; Httppostedfile postfile = File[formfield]; 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; Get 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 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 (string. Format ("Sorry to upload the file too large, cannot exceed {0}m! ", Setmaxsizem)); }}///<summary>//Get the Catalogue///</summary>//<RETURNS></RETURNS&G T 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 Directory if (directory.exists (dir) = = False) Directory.CreateDirectory (dir); return dir; }///<summary>///Verify file type///</summary>//<param name= "FileName" ></pa ram> private void CheckIngtype (String FileName) {//Gets the Allow upload Type list string[] Typelist = this. Setfiletype.split (', '); Gets the upload file type (lowercase) String type = Path.getextension (FileName). Tolowerinvariant (); String Name = Path.getfilenamewithoutextension (FileName); String namehash = Name.gethashcode (). ToString (); Verify the type if (typelist.contains (type) = = False) this. Tryerror ("File type illegal!"); }///<summary>//////</summary>//<param name= "MSG" ></param> private void Tryerror (String msg) {this. GetError = true; This. GetMessage = msg; } }}
ASP. net file upload class easy to use