ASP.NET MVC HttpPostedFileBase文件上传

HttpPostedFileBase文件上传,支持多文件一次上传,如有图片,则支持略缩图保存

文件传输信息封装

 1    /// <summary>
 2     /// 文件生成方式
 3     /// </summary>
 4     public class UpFileMessage
 5     {
 6         /// <summary>
 7         /// 文件名
 8         /// </summary>
 9         public string OriginalFileName { get; set; }
10 
11         /// <summary>
12         /// 是否保存略缩图
13         /// </summary>
14         public bool IsImage { get; set; }
15 
16         /// <summary>
17         /// 文件流
18         /// </summary>
19         public Stream FileStream { get; set; }
20 
21         /// <summary>
22         /// 生成缩略图的方式
23         /// [WH]-指定宽高
24         /// [H]-指定高,按比例缩放宽
25         /// [W]-指定宽,按比例缩放高
26         /// </summary>
27         public string Mode { get; set; }
28 
29         /// <summary>
30         /// 略缩图高度
31         /// </summary>
32         public int? ThumbHeight { get; set; }
33 
34         /// <summary>
35         /// 略缩图宽度
36         /// </summary>
37         public int? ThumbWidth { get; set; }
38 
39     }
View Code

文件上传返回结果

 1   /// <summary>
 2     /// 文件上传返回结果
 3     /// </summary>
 4     public class UpFileResultMessage
 5     {
 6         /// <summary>
 7         /// 文件保存是否成功
 8         /// </summary>
 9         public bool IsSuccess { get; set; }
10 
11         /// <summary>
12         /// 错误消息
13         /// </summary>
14         public string Message { get; set; }
15 
16         /// <summary>
17         /// 原始文件名-(无扩展名)
18         /// </summary>
19         public string FileName { get; set; }
20 
21         /// <summary>
22         /// 文件名扩展名
23         /// </summary>
24         public string FileSuffix { get; set; }
25 
26         /// <summary>
27         /// 文件名保存路径
28         /// </summary>
29         public string FilePath { get; set; }
30 
31         /// <summary>
32         /// 文件类型为图片时
33         /// 缩略图保存路径
34         /// </summary>
35         public string ThumbPath { get; set; }
36 
37         /// <summary>
38         /// 保存文件名(无扩展名)
39         /// </summary>
40         public string SaveFileName { get; set; }
41 
42         /// <summary>
43         /// 文件自增ID
44         /// </summary>
45         public int[] FileIdArray { get; set; }
46     }
View Code

文件上传类库

需引用System.Web命名空间,并对 [System.Web.UI.Page] 进行继承,调用Server.MapPath方法

  1 public class FileUtil : System.Web.UI.Page
  2     {
  3         /// <summary>
  4         /// 图片上传
  5         /// </summary>
  6         /// <param name="fileMessage">文件生成方式</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                 //获取文件扩展名
 16                 var fileSuffix = Path.GetExtension(fileMessage.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                 //保存文件名
 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 = fileSuffix,
 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                         while ((count = sourceStream.Read(buffer, 0, bufferLen)) > 0)
 47                         {
 48                             targetStream.Write(buffer, 0, count);
 49                         }
 50                     }
 51                     //上传文件为图片时,需创建缩略图
 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                             switch (fileMessage.Mode)
 68                             {
 69                                 case "WH":  //指定高宽缩放(可能变形)  
 70                                     thumbWidth = fileMessage.ThumbWidth.HasValue ? fileMessage.ThumbWidth.Value : 200;
 71                                     thumbHeight = fileMessage.ThumbHeight.HasValue ? fileMessage.ThumbHeight.Value : 200;
 72                                     break;
 73                                 case "W":   //指定宽,高按比例     
 74                                     thumbWidth = fileMessage.ThumbWidth.HasValue ? fileMessage.ThumbWidth.Value : 200;
 75                                     thumbHeight = height * thumbWidth / width;
 76                                     break;
 77                                 case "H":   //指定高,宽按比例
 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         /// 创建指定图片文件流的缩略图
104         /// </summary>
105         /// <param name="thumbFilePath">缩略图文件保存路径</param>
106         /// <param name="fileStream">原始文件流</param>
107         /// <param name="width">缩略图宽</param>
108         /// <param name="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

调用方式

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

 代码地址:文件上传类库包.zip

原文地址:https://www.cnblogs.com/shanshanlaichi/p/7212301.html