查看: 2102|回复: 0

[.NET开发] ASP.NET MVC HttpPostedFileBase文件上传的实例代码

发表于 2018-1-2 08:00:00

本文介绍了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. /// <summary>
  11. /// 是否保存略缩图
  12. /// </summary>
  13. public bool IsImage { get; set; }
  14. /// <summary>
  15. /// 文件流
  16. /// </summary>
  17. public Stream FileStream { get; set; }
  18. /// <summary>
  19. /// 生成缩略图的方式
  20. /// [WH]-指定宽高
  21. /// [H]-指定高,按比例缩放宽
  22. /// [W]-指定宽,按比例缩放高
  23. /// </summary>
  24. public string Mode { get; set; }
  25. /// <summary>
  26. /// 略缩图高度
  27. /// </summary>
  28. public int? ThumbHeight { get; set; }
  29. /// <summary>
  30. /// 略缩图宽度
  31. /// </summary>
  32. public int? ThumbWidth { get; set; }
  33. }
复制代码

文件上传返回结果

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

文件上传类库

需引用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. var fileSuffix = Path.GetExtension(fileMessage.OriginalFileName);
  16. var uploadFolder = Path.Combine(System.Web.HttpContext.Current.Server.MapPath(ParmsConfig.UpFilePathUrl), now);
  17. if (!Directory.Exists(uploadFolder))
  18. {
  19. Directory.CreateDirectory(uploadFolder);
  20. }
  21. //保存文件名
  22. string saveFileName = guid + fileSuffix;
  23. string filePath = Path.Combine(uploadFolder, saveFileName);
  24. UpFileResultMessage upFileResult = new UpFileResultMessage()
  25. {
  26. IsSuccess = true,
  27. FileName = Path.GetFileNameWithoutExtension(fileMessage.OriginalFileName),
  28. FileSuffix = fileSuffix,
  29. FilePath = string.Format(@"{0}/小贝", ParmsConfig.UpFileIPAddressUrl, now),
  30. SaveFileName = guid
  31. };
  32. using (Stream sourceStream = fileMessage.FileStream)
  33. {
  34. using (FileStream targetStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None))
  35. {
  36. const int bufferLen = 1024 * 4;//4KB
  37. byte[] buffer = new byte[bufferLen];
  38. int count = 0;
  39. while ((count = sourceStream.Read(buffer, 0, bufferLen)) > 0)
  40. {
  41. targetStream.Write(buffer, 0, count);
  42. }
  43. }
  44. //上传文件为图片时,需创建缩略图
  45. if (fileMessage.IsImage)
  46. {
  47. var uploadThumbFolder = Path.Combine(uploadFolder, "Thumb");
  48. if (!Directory.Exists(uploadThumbFolder))
  49. {
  50. Directory.CreateDirectory(uploadThumbFolder);
  51. }
  52. using (FileStream targetStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.None))
  53. {
  54. System.Drawing.Image image = System.Drawing.Image.FromStream(targetStream);
  55. int width = image.Width;
  56. int height = image.Height;
  57. int thumbWidth = 0;
  58. int thumbHeight = 0;
  59. switch (fileMessage.Mode)
  60. {
  61. case "WH": //指定高宽缩放(可能变形)
  62. thumbWidth = fileMessage.ThumbWidth.HasValue ? fileMessage.ThumbWidth.Value : 200;
  63. thumbHeight = fileMessage.ThumbHeight.HasValue ? fileMessage.ThumbHeight.Value : 200;
  64. break;
  65. case "W": //指定宽,高按比例
  66. thumbWidth = fileMessage.ThumbWidth.HasValue ? fileMessage.ThumbWidth.Value : 200;
  67. thumbHeight = height * thumbWidth / width;
  68. break;
  69. case "H": //指定高,宽按比例
  70. thumbHeight = fileMessage.ThumbHeight.HasValue ? fileMessage.ThumbHeight.Value : 200;
  71. thumbWidth = width * thumbHeight / height;
  72. break;
  73. default:
  74. thumbWidth = fileMessage.ThumbWidth.HasValue ? fileMessage.ThumbWidth.Value : 200;
  75. thumbHeight = height * thumbWidth / width;
  76. break;
  77. }
  78. string thumbFilePath = Path.Combine(uploadThumbFolder, saveFileName);
  79. CreateThumbnail(thumbFilePath, targetStream, thumbWidth, thumbHeight);
  80. upFileResult.ThumbPath = string.Format(@"{0}/小贝/Thumb", ParmsConfig.UpFileIPAddressUrl, now);
  81. }
  82. }
  83. }
  84. return upFileResult;
  85. }
  86. catch (Exception ex)
  87. {
  88. return new UpFileResultMessage() { IsSuccess = false, Message = ex.Message };
  89. }
  90. }
  91. /// <summary>
  92. /// 创建指定图片文件流的缩略图
  93. /// </summary>
  94. /// <param name="thumbFilePath">缩略图文件保存路径</param>
  95. /// <param name="fileStream">原始文件流</param>
  96. /// <param name="width">缩略图宽</param>
  97. /// <param name="height">缩略图高</param>
  98. private void CreateThumbnail(string thumbFilePath, Stream fileStream, int width, int height)
  99. {
  100. using (Image image = Image.FromStream(fileStream))
  101. {
  102. using (Image thumbnail = image.GetThumbnailImage(width, height, null, IntPtr.Zero))
  103. {
  104. thumbnail.Save(thumbFilePath);
  105. }
  106. }
  107. }
  108. }
复制代码

调用方式

  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);
复制代码

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持程序员之家。



回复

使用道具 举报