ASP.NET MVC HttpPostedFileBase文件上传的实例代码
本文介绍了ASP.NETMVCHttpPostedFileBase文件上传,分享给大家,希望对大家有帮助
HttpPostedFileBase文件上传,支持多文件一次上传,如有图片,则支持略缩图保存
文件传输信息封装
//////文件生成方式 /// publicclassUpFileMessage { //////文件名 /// publicstringOriginalFileName{get;set;} //////是否保存略缩图 /// publicboolIsImage{get;set;} //////文件流 /// publicStreamFileStream{get;set;} //////生成缩略图的方式 ///[WH]-指定宽高 ///[H]-指定高,按比例缩放宽 ///[W]-指定宽,按比例缩放高 /// publicstringMode{get;set;} //////略缩图高度 /// publicint?ThumbHeight{get;set;} //////略缩图宽度 /// publicint?ThumbWidth{get;set;} }
文件上传返回结果
//////文件上传返回结果 /// publicclassUpFileResultMessage { //////文件保存是否成功 /// publicboolIsSuccess{get;set;} //////错误消息 /// publicstringMessage{get;set;} //////原始文件名-(无扩展名) /// publicstringFileName{get;set;} //////文件名扩展名 /// publicstringFileSuffix{get;set;} //////文件名保存路径 /// publicstringFilePath{get;set;} //////文件类型为图片时 ///缩略图保存路径 /// publicstringThumbPath{get;set;} //////保存文件名(无扩展名) /// publicstringSaveFileName{get;set;} //////文件自增ID /// publicint[]FileIdArray{get;set;} }
文件上传类库
需引用System.Web命名空间,并对[System.Web.UI.Page]进行继承,调用Server.MapPath方法
publicclassFileUtil:System.Web.UI.Page { //////图片上传 /// ///文件生成方式 /// publicUpFileResultMessageUpLoadFile(UpFileMessagefileMessage) { try { stringnow=DateTime.Today.ToString("yyyyMMdd"); stringguid=Guid.NewGuid().ToString(); //获取文件扩展名 varfileSuffix=Path.GetExtension(fileMessage.OriginalFileName); varuploadFolder=Path.Combine(System.Web.HttpContext.Current.Server.MapPath(ParmsConfig.UpFilePathUrl),now); if(!Directory.Exists(uploadFolder)) { Directory.CreateDirectory(uploadFolder); } //保存文件名 stringsaveFileName=guid+fileSuffix; stringfilePath=Path.Combine(uploadFolder,saveFileName); UpFileResultMessageupFileResult=newUpFileResultMessage() { IsSuccess=true, FileName=Path.GetFileNameWithoutExtension(fileMessage.OriginalFileName), FileSuffix=fileSuffix, FilePath=string.Format(@"{0}/{1}",ParmsConfig.UpFileIPAddressUrl,now), SaveFileName=guid }; using(StreamsourceStream=fileMessage.FileStream) { using(FileStreamtargetStream=newFileStream(filePath,FileMode.Create,FileAccess.Write,FileShare.None)) { constintbufferLen=1024*4;//4KB byte[]buffer=newbyte[bufferLen]; intcount=0; while((count=sourceStream.Read(buffer,0,bufferLen))>0) { targetStream.Write(buffer,0,count); } } //上传文件为图片时,需创建缩略图 if(fileMessage.IsImage) { varuploadThumbFolder=Path.Combine(uploadFolder,"Thumb"); if(!Directory.Exists(uploadThumbFolder)) { Directory.CreateDirectory(uploadThumbFolder); } using(FileStreamtargetStream=newFileStream(filePath,FileMode.Open,FileAccess.Read,FileShare.None)) { System.Drawing.Imageimage=System.Drawing.Image.FromStream(targetStream); intwidth=image.Width; intheight=image.Height; intthumbWidth=0; intthumbHeight=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; } stringthumbFilePath=Path.Combine(uploadThumbFolder,saveFileName); CreateThumbnail(thumbFilePath,targetStream,thumbWidth,thumbHeight); upFileResult.ThumbPath=string.Format(@"{0}/{1}/Thumb",ParmsConfig.UpFileIPAddressUrl,now); } } } returnupFileResult; } catch(Exceptionex) { returnnewUpFileResultMessage(){IsSuccess=false,Message=ex.Message}; } } /// ///创建指定图片文件流的缩略图 /// ///缩略图文件保存路径 /// 原始文件流 /// 缩略图宽 /// 缩略图高 privatevoidCreateThumbnail(stringthumbFilePath,StreamfileStream,intwidth,intheight) { using(Imageimage=Image.FromStream(fileStream)) { using(Imagethumbnail=image.GetThumbnailImage(width,height,null,IntPtr.Zero)) { thumbnail.Save(thumbFilePath); } } } }
调用方式
varupFileMsg=newUpFileMessage() { IsImage=true, OriginalFileName=platformImg[i].FileName, FileStream=platformImg[i].InputStream, ThumbWidth=ThumbWidth, Mode="W" }; varupFileResultMsg=newFileUtil().UpLoadFile(upFileMsg);
代码地址:文件上传类库包.zip
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。