This article mainly introduces the ASP. NET MVC httppostedfilebase file upload, has a certain reference value, interested in small partners can refer to
This article introduces the ASP. NET MVC httppostedfilebase file upload, share to everybody, hope to be helpful to everybody
HttpPostedFileBase file Upload, support multi-file upload, if there is a picture, support thumbnail save
File Transfer Information Encapsulation
<summary>//// file generation method/// </summary> public class Upfilemessage {// <summary >///File name/// </summary> public string OriginalFilename {get; set;} <summary>//whether to save thumbnails/// </summary> public bool Isimage {get; set;} <summary>///file stream/// </summary> public stream FileStream {get; set;} <summary>///How to generate thumbnails/// [wh]-specified width///[ h]-specified high, proportionally relaxed// [w]-specified width, scaled high // </summary> public string Mode {get; set;} <summary>///thumbnail height//// </summary> public int? thumbheight {get; set;} <summary>///thumbnail width/// </summary> public int? thumbwidth {get; set;} }
File Upload returns results
<summary>/// File Upload returns results/// </summary> public class Upfileresultmessage { // <summary>///whether the file was saved successfully/// </summary> public bool Issuccess {get; set;} <summary>///error message/// </summary> public string Message {get; set;} <summary> ///Original file name-(no extension)/// </summary> public string FileName {get; set;} <summary>/// file name extension/// </summary> public string Filesuffix {get; set;} <summary>//// file name save path/// </summary> public string FilePath {get; set;} <summary>// The file type is picture/// thumbnail save path///</summary> public string Thumbpath {get; Set } ///<summary>///Save file name (no extension)/// </summary> public string Savefilename {get; set;} ///<summary>/// file self-increment ID///</summary> public int[] Fileidarray {get; set;} }
File Upload class Library
You need to reference the System.Web namespace and inherit [System.Web.UI.Page], calling the Server.MapPath method
public class FileUtil:System.Web.UI.Page {//<summary>///image Upload///</summary>//<param Name= "Filemessage" > File generation Method </param>//<returns></returns> public upfileresultmessage Uploadfil E (upfilemessage filemessage) {try {string now = DateTime.Today.ToString ("YyyyMMdd"); String guid = Guid.NewGuid (). ToString (); Get file name extension var filesuffix = path.getextension (filemessage.originalfilename); var uploadfolder = Path.Combine (System.Web.HttpContext.Current.Server.MapPath (Parmsconfig.upfilepathurl), now); if (! Directory.Exists (Uploadfolder)) {directory.createdirectory (Uploadfolder); }//Save file name string Savefilename = GUID + filesuffix; String filePath = Path.Combine (Uploadfolder, savefilename); Upfileresultmessage Upfileresult = new Upfileresultmessage () {issuccess = true, FileName = Path. GetfilenamewithoutexteNsion (filemessage.originalfilename), Filesuffix = Filesuffix, FilePath = string. Format (@ "{0}/{1}", Parmsconfig.upfileipaddressurl, now), Savefilename = GUID}; using (Stream Sourcestream = Filemessage.filestream) {using (FileStream Targetstream = new FileStream (fil Epath, FileMode.Create, FileAccess.Write, Fileshare.none)) {const int bufferlen = 1024x768 * 4;//4KB byte[] buffer = new Byte[bufferlen]; int count = 0; while ((count = sourcestream.read (buffer, 0, Bufferlen)) > 0) {targetstream.write (buffer, 0, C Ount); }}//When uploading a file as a picture, you need to create a thumbnail if (filemessage.isimage) {var uploadthumbfolder = Pa Th. Combine (Uploadfolder, "Thumb"); if (! Directory.Exists (Uploadthumbfolder)) {directory.createdirectory (Uploadthumbfolder); } using (FileStream targetstream = new FileStream (FilePath, FileMode.Open, FileAccess.Read, Fileshare.none)) {System.Drawing.Im Age image = System.Drawing.Image.FromStream (Targetstream); int width = image. Width; int height = image. Height; int thumbwidth = 0; int thumbheight = 0; Switch (filemessage.mode) {case "WH"://Specify aspect scaling (possibly deformed) Thumbwidth = Filemessa Ge. Thumbwidth.hasvalue? filemessage.thumbwidth.value:200; Thumbheight = FileMessage.ThumbHeight.HasValue? filemessage.thumbheight.value:200; Break Case "W"://Specify width, high proportionally thumbwidth = FileMessage.ThumbWidth.HasValue? filemessage.thumbwidth.value:200; Thumbheight = height * thumbwidth/width; Break Case "H"://Specify high, wide proportionally thumbheight = FileMessage.ThumbHeight.HasValue? Filemessage.thumbheight.value:200; Thumbwidth = width * thumbheight/height; Break Default:thumbwidth = FileMessage.ThumbWidth.HasValue? filemessage.thumbwidth.value:200; Thumbheight = height * thumbwidth/width; Break } String thumbfilepath = Path.Combine (Uploadthumbfolder, savefilename); Createthumbnail (Thumbfilepath, Targetstream, Thumbwidth, thumbheight); Upfileresult.thumbpath = string. Format (@ "{0}/{1}/thumb", Parmsconfig.upfileipaddressurl, now); }}} return upfileresult; } catch (Exception ex) {return new Upfileresultmessage () {issuccess = false, Message = ex. Message}; }}///<summary>//Create thumbnails of the specified picture file stream///</summary>//<param name= "Thumbfilepath" > Thumbnail files Save path </param>//<param name= "FileStream" > Raw file Stream </param>//<param Name= "width" > Thumbnail width </param>//<param name= "height" > thumbnail height </param> private void Createthumbnail (s Tring Thumbfilepath, Stream fileStream, int width, int height) {using (Image image = Image.fromstream (FileStream) ) {using (Image thumbnail = image. Getthumbnailimage (width, height, null, IntPtr.Zero)) {thumbnail. Save (Thumbfilepath); } } } }
Invocation mode
var upfilemsg = new Upfilemessage () { Isimage = true, originalfilename = Platformimg[i]. FileName, FileStream = Platformimg[i]. InputStream, thumbwidth = thumbwidth, Mode = "W" }; var upfileresultmsg = new Fileutil (). UploadFile (UPFILEMSG);