本文介绍了ASP.NET MVC httpPostedfileBase文件上传 ,分享给大家,希望对大家有帮助
httpPostedfileBase文件上传,支持多文件一次上传,如有图片,则支持略缩图保存
文件传输信息封装
/// <summary> /// 文件生成方式 /// </summary> public class UpfileMessage { /// <summary> /// 文件名 /// </summary> public string Originalfilename { get; set; } /// <summary> /// 是否保存略缩图 /// </summary> public bool IsImage { get; set; } /// <summary> /// 文件流 /// </summary> public Stream fileStream { get; set; } /// <summary> /// 生成缩略图的方式 /// [WH]-指定宽高 /// [H]-指定高,按比例缩放宽 /// [W]-指定宽,按比例缩放高 /// </summary> public string Mode { get; set; } /// <summary> /// 略缩图高度 /// </summary> public int? ThumbHeight { get; set; } /// <summary> /// 略缩图宽度 /// </summary> public int? ThumbWIDth { get; set; } }
文件上传返回结果
/// <summary> /// 文件上传返回结果 /// </summary> public class UpfileResultMessage { /// <summary> /// 文件保存是否成功 /// </summary> public bool IsSuccess { get; set; } /// <summary> /// 错误消息 /// </summary> public string Message { get; set; } /// <summary> /// 原始文件名-(无扩展名) /// </summary> public string filename { get; set; } /// <summary> /// 文件名扩展名 /// </summary> public string fileSuffix { get; set; } /// <summary> /// 文件名保存路径 /// </summary> public string filePath { get; set; } /// <summary> /// 文件类型为图片时 /// 缩略图保存路径 /// </summary> public string ThumbPath { get; set; } /// <summary> /// 保存文件名(无扩展名) /// </summary> public string Savefilename { get; set; } /// <summary> /// 文件自增ID /// </summary> public int[] fileIDArray { get; set; } }
文件上传类库
需引用System.Web命名空间,并对 [System.Web.UI.Page] 进行继承,调用Server.MapPath方法
public class fileUtil : System.Web.UI.Page { /// <summary> /// 图片上传 /// </summary> /// <param name="fileMessage">文件生成方式</param> /// <returns></returns> public UpfileResultMessage UpLoadfile(UpfileMessage fileMessage) { try { string Now = DateTime.Today.ToString("yyyyMMdd"); string guID = GuID.NewGuID().ToString(); //获取文件扩展名 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); } //保存文件名 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(filePath, fileMode.Create, fileAccess.Write, fileShare.None)) { const int bufferLen = 1024 * 4;//4KB byte[] buffer = new byte[bufferLen]; int count = 0; while ((count = sourceStream.Read(buffer, 0, bufferLen)) > 0) { targetStream.Write(buffer, 0, count); } } //上传文件为图片时,需创建缩略图 if (fileMessage.IsImage) { var uploadThumbFolder = Path.Combine(uploadFolder, "Thumb"); if (!Directory.Exists(uploadThumbFolder)) { Directory.CreateDirectory(uploadThumbFolder); } using (fileStream targetStream = new fileStream(filePath, fileMode.Open, fileAccess.Read, fileShare.None)) { System.Drawing.Image 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": //指定高宽缩放(可能变形) thumbWIDth = fileMessage.ThumbWIDth.HasValue ? fileMessage.ThumbWIDth.Value : 200; thumbHeight = fileMessage.ThumbHeight.HasValue ? fileMessage.ThumbHeight.Value : 200; break; case "W": //指定宽,高按比例 thumbWIDth = fileMessage.ThumbWIDth.HasValue ? fileMessage.ThumbWIDth.Value : 200; thumbHeight = height * thumbWIDth / wIDth; break; case "H": //指定高,宽按比例 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> /// 创建指定图片文件流的缩略图 /// </summary> /// <param name="thumbfilePath">缩略图文件保存路径</param> /// <param name="fileStream">原始文件流</param> /// <param name="wIDth">缩略图宽</param> /// <param name="height">缩略图高</param> private voID Createthumbnail(string 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); } } } }
调用方式
var upfileMsg = new UpfileMessage() { IsImage = true, Originalfilename = platformimg[i].filename, fileStream = platformimg[i].inputStream, ThumbWIDth = ThumbWIDth, Mode = "W" }; var upfileResultMsg = new fileUtil().UpLoadfile(upfileMsg);总结
以上是内存溢出为你收集整理的HttpPostedFileBase文件上传实例详解全部内容,希望文章能够帮你解决HttpPostedFileBase文件上传实例详解所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)