ASP. net mvc HttpPostedFileBase file upload,
HttpPostedFileBase allows you to upload multiple files at a time. If you have images, you can save the thumbnail.
File Transfer Information Encapsulation
1 /// <summary> 2 // file generation method 3 /// </summary> 4 public class UpFileMessage 5 {6 /// <summary> 7 // file name 8 /// </summary> 9 public string OriginalFileName {get; set;} 10 11 /// <summary> 12 // whether to save the thumbnail. 13 /// </summary> 14 public bool IsImage {get; set ;} 15 16 /// <summary> 17 // file Stream 18 /// </summary> 19 public Stream FileStream {get; set ;} 20 21 /// <summary> 22 /// method of generating the thumbnail 23 /// [WH]-specify the width and height 24 /// [H] -Specify height, scale proportionally to 25 /// [W]-specify width, scale proportionally to 26 /// </summary> 27 public string Mode {get; set;} 28 29 /// <summary> 30 /// thumbnail height 31 /// </summary> 32 public int? ThumbHeight {get; set;} 33 34 /// <summary> 35 /// thumbnail width 36 /// </summary> 37 public int? ThumbWidth {get; set;} 38 39}
View Code
File Upload return results
1 /// <summary> 2 // File Upload return result 3 /// </summary> 4 public class UpFileResultMessage 5 {6 /// <summary> 7 /// whether the file is successfully saved 8 // </summary> 9 public bool IsSuccess {get; set;} 10 11 /// <summary> 12 // error Message 13 /// </summary> 14 public string Message {get; set ;} 15 16 /// <summary> 17 // original file name-(no extension) 18 /// </summary> 19 public string FileName {get; set ;} 20 21 /// <summary> 22 // file name extension 23 /// </summary> 24 public string FileSuffix {get; set ;} 25 26 /// <summary> 27 // file name storage path 28 /// </summary> 29 public string FilePath {get; set ;} 30 31 /// <summary> 32 // when the file type is image 33 /// the thumbnail storage path 34 /// </summary> 35 public string ThumbPath {get; set;} 36 37 // <summary> 38 // save the file name (without extension) 39 // </summary> 40 public string SaveFileName {get; set ;} 41 42 // <summary> 43 // File Auto-increment ID44 // </summary> 45 public int [] FileIdArray {get; set;} 46}
View Code
File Upload class library
You must reference the System. Web namespace and inherit from [System. Web. UI. Page] to call the Server. MapPath method.
1 public class FileUtil: System. web. UI. page 2 {3 /// <summary> 4 /// Image Upload 5 /// </summary> 6 /// <param name = "fileMessage"> file generation method </param> 7 // <returns> </returns> 8 public UpFileResultMessage UpLoadFile (UpFileMessage fileMessage) 9 {10 try 11 {12 string now = DateTime. today. toString ("yyyyMMdd"); 13 string guid = Guid. newGuid (). toString (); 14 15 // get the file extension 16 var fileSuffix = Path. getExtension (f IleMessage. OriginalFileName); 17 18 var uploadFolder = Path. Combine (System. Web. HttpContext. Current. Server. MapPath (ParmsConfig. UpFilePathUrl), now); 19 20 if (! Directory. exists (uploadFolder) 21 {22 Directory. createDirectory (uploadFolder); 23} 24 25 // save the file name 26 string saveFileName = guid + fileSuffix; 27 string filePath = Path. combine (uploadFolder, saveFileName); 28 29 30 UpFileResultMessage upFileResult = new UpFileResultMessage () 31 {32 IsSuccess = true, 33 FileName = Path. getFileNameWithoutExtension (fileMessage. originalFileName), 34 FileSuffix = file Suffix, 35 FilePath = string. format (@ "{0}/{1}", ParmsConfig. upFileIPAddressUrl, now), 36 SaveFileName = guid 37}; 38 39 using (Stream sourceStream = fileMessage. fileStream) 40 {41 using (FileStream targetStream = new FileStream (filePath, FileMode. create, FileAccess. write, FileShare. none) 42 {43 const int bufferLen = 1024*4; // 4KB 44 byte [] buffer = new byte [bufferLen]; 45 int count = 0; 46 wh Ile (count = sourceStream. read (buffer, 0, bufferLen)> 0) 47 {48 targetStream. write (buffer, 0, count); 49} 50} 51 // create a thumbnail 52 if (fileMessage. isImage) 53 {54 var uploadThumbFolder = Path. combine (uploadFolder, "Thumb"); 55 56 if (! Directory. exists (uploadThumbFolder) 57 {58 Directory. createDirectory (uploadThumbFolder); 59} 60 using (FileStream targetStream = new FileStream (filePath, FileMode. open, FileAccess. read, FileShare. none) 61 {62 System. drawing. image image = System. drawing. image. fromStream (targetStream); 63 int width = image. width; 64 int height = image. height; 65 int thumbWidth = 0; 66 int thumbHeight = 0; 67 swi Tch (fileMessage. Mode) 68 {69 case "WH": // specify the height/width Scaling (may be deformed) 70 thumbWidth = fileMessage. ThumbWidth. HasValue? FileMessage. ThumbWidth. Value: 200; 71 thumbHeight = fileMessage. ThumbHeight. HasValue? FileMessage. ThumbHeight. Value: 200; 72 break; 73 case "W": // specify the width. The height is proportional to 74 thumbWidth = fileMessage. ThumbWidth. HasValue? FileMessage. thumbWidth. value: 200; 75 thumbHeight = height * thumbWidth/width; 76 break; 77 case "H": // specify the height, width in proportion to 78 thumbHeight = fileMessage. thumbHeight. hasValue? FileMessage. ThumbHeight. Value: 200; 79 thumbWidth = width * thumbHeight/height; 80 break; 81 default: 82 thumbWidth = fileMessage. ThumbWidth. HasValue? FileMessage. thumbWidth. value: 200; 83 thumbHeight = height * thumbWidth/width; 84 break; 85} 86 string thumbFilePath = Path. combine (uploadThumbFolder, saveFileName); 87 CreateThumbnail (thumbFilePath, targetStream, thumbWidth, thumbHeight); 88 upFileResult. thumbPath = string. format (@ "{0}/{1}/Thumb", ParmsConfig. upFileIPAddressUrl, now); 89} 90} 91} 92 93 return upFileResult; 94} 95 catch (Exception ex) 96 {97 return new UpFileResultMessage () {IsSuccess = false, message = ex. message }; 98} 99 100} 101 102 // <summary> 103 // create a thumbnail of the specified image file stream 104 /// </summary> 105 // <param name = "thumbFilePath"> saved path of the thumbnail file </param> 106 // <param name = "fileStream"> original file stream </param> 107 // <param name =" width "> thumbnail width </param> 108 /// <param name =" height "> thumbnail height </param> 109 private void CreateThumbnail (string thumbFilePath, stream fileStream, int width, int height) 110 {111 using (Image image = Image. fromStream (fileStream) 112 {113 using (Image thumbnail = image. getThumbnailImage (width, height, null, IntPtr. zero) 114 {115 thumbnail. save (thumbFilePath); 116} 117} 118 119} 120 121}
View Code
Call Method
1 var upFileMsg = new UpFileMessage()2 {3 IsImage = true,4 OriginalFileName = platformImg[i].FileName,5 FileStream = platformImg[i].InputStream,6 ThumbWidth = ThumbWidth,7 Mode = "W"8 };9 var upFileResultMsg = new FileUtil().UpLoadFile(upFileMsg);
View Code
Code address: File Upload class library package. Zip