实例介绍
【实例简介】
【实例截图】
【实例截图】
【核心代码】SwfUpload在MVC4下多文件上传缩略图水印例子
using System; using System.Collections; using System.Web; using System.IO; using System.Drawing; using System.Net; namespace SwfUpload.Core { public class siteconfig { public int attachfilesize{ get; set;}//附件文件大小 public int attachimgsize { get; set; }//附件图片大小 public int attachimgmaxheight{ get; set;}//附件图片最大高度 public int attachimgmaxwidth { get; set; }//附件图片最大宽度 public int thumbnailwidth { get; set; }//缩略图宽度 public int thumbnailheight { get; set; }//缩略图宽度 public int watermarktype { get; set; }//水印类型1文件2图片 public string watermarktext { get; set; }//水印文字 public int watermarkimgquality { get; set; }//水印质量 public string watermarkpic { get; set; }//水印图片名称 public string webpath { get; set; }//web目录 public string attachpath { get; set; }//上传文件夹 public int watermarkposition { get;set;}//水印位置 public string watermarkfont { get; set; }//水印字体 public int watermarkfontsize { get; set; }//水印字体大小 public int watermarktransparency { get; set; }//透明度 public int attachsave { get; set; }//保存的类型按年月/日存入不同的文件夹/按年月日每天一个文件夹 public string attachextension { get; set; }//允许的扩展名 } public class UpLoad { private siteconfig siteConfig; public UpLoad() { siteConfig = new siteconfig() { attachfilesize =51200, attachimgsize =10240, attachimgmaxheight =800, attachimgmaxwidth = 800, thumbnailwidth=300, thumbnailheight=300, watermarktype=1, watermarkimgquality=80, watermarkpic = "watermark.png", webpath="/", attachpath = "Upload", watermarkposition =9, watermarkfont = "Tahoma", watermarkfontsize=12, watermarktransparency=5, attachsave =1, attachextension = "gif,jpg,png,bmp,rar,zip,doc,xls,txt" }; } /// <summary> /// 裁剪图片并保存 /// </summary> public bool cropSaveAs(string fileName, string newFileName, int maxWidth, int maxHeight, int cropWidth, int cropHeight, int X, int Y) { string fileExt = Common.GetFileExt(fileName); //文件扩展名,不含“.” if (!IsImage(fileExt)) { return false; } string newFileDir = Common.GetMapPath(newFileName.Substring(0, newFileName.LastIndexOf(@"/") 1)); //检查是否有该路径,没有则创建 if (!Directory.Exists(newFileDir)) { Directory.CreateDirectory(newFileDir); } try { string fileFullPath = Common.GetMapPath(fileName); string toFileFullPath = Common.GetMapPath(newFileName); return Thumbnail.MakeThumbnailImage(fileFullPath, toFileFullPath, 180, 180, cropWidth, cropHeight, X, Y); } catch { return false; } } /// <summary> /// 文件上传方法A /// </summary> /// <param name="postedFile">文件流</param> /// <param name="isThumbnail">是否生成缩略图</param> /// <param name="isWater">是否打水印</param> /// <returns>服务器文件路径</returns> public string fileSaveAs(HttpPostedFile postedFile, bool isThumbnail, bool isWater) { return fileSaveAs(postedFile, isThumbnail, isWater, false, false); } /// <summary> /// 文件上传方法B /// </summary> /// <param name="postedFile">文件流</param> /// <param name="isThumbnail">是否生成缩略图</param> /// <param name="isWater">是否打水印</param> /// <param name="isImage">是否必须上传图片</param> /// <returns>服务器文件路径</returns> public string fileSaveAs(HttpPostedFile postedFile, bool isThumbnail, bool isWater, bool _isImage) { return fileSaveAs(postedFile, isThumbnail, isWater, _isImage, false); } /// <summary> /// 文件上传方法C /// </summary> /// <param name="postedFile">文件流</param> /// <param name="isThumbnail">是否生成缩略图</param> /// <param name="isWater">是否打水印</param> /// <param name="isReOriginal">是否返回文件原名称</param> /// <returns>服务器文件路径</returns> public string fileSaveAs(HttpPostedFile postedFile, bool isThumbnail, bool isWater, bool _isImage, bool _isReOriginal) { try { string fileExt = Common.GetFileExt(postedFile.FileName); //文件扩展名,不含“.” string originalFileName = postedFile.FileName.Substring(postedFile.FileName.LastIndexOf(@"\") 1); //取得文件原名 string fileName = Common.GetRamCode() "." fileExt; //随机文件名 string dirPath = GetUpLoadPath(); //上传目录相对路径 //检查文件扩展名是否合法 if (!CheckFileExt(fileExt)) { return "{\"msg\": \"0\", \"msgbox\": \"不允许上传" fileExt "类型的文件!\"}"; } //检查是否必须上传图片 if (_isImage && !IsImage(fileExt)) { return "{\"msg\": \"0\", \"msgbox\": \"对不起,仅允许上传图片文件!\"}"; } //检查文件大小是否合法 if(!CheckFileSize(fileExt, postedFile.ContentLength)) { return "{\"msg\": \"0\", \"msgbox\": \"文件超过限制的大小啦!\"}"; } //获得要保存的文件路径 string serverFileName = dirPath fileName; string serverThumbnailFileName = dirPath "small_" fileName; string returnFileName = serverFileName; //物理完整路径 string toFileFullPath = Common.GetMapPath(dirPath); //检查有该路径是否就创建 if (!Directory.Exists(toFileFullPath)) { Directory.CreateDirectory(toFileFullPath); } //保存文件 postedFile.SaveAs(toFileFullPath fileName); //如果是图片,检查图片尺寸是否超出限制 if (IsImage(fileExt) && (this.siteConfig.attachimgmaxheight > 0 || this.siteConfig.attachimgmaxwidth > 0)) { Thumbnail.MakeThumbnailImage(toFileFullPath fileName, toFileFullPath fileName, this.siteConfig.attachimgmaxwidth, this.siteConfig.attachimgmaxheight); } //是否生成缩略图 if (IsImage(fileExt) && isThumbnail && this.siteConfig.thumbnailwidth > 0 && this.siteConfig.thumbnailheight > 0) { Thumbnail.MakeThumbnailImage(toFileFullPath fileName, toFileFullPath "small_" fileName, this.siteConfig.thumbnailwidth, this.siteConfig.thumbnailheight, "Cut"); returnFileName = "," serverThumbnailFileName; //返回缩略图,以逗号分隔开 } //是否打图片水印 if (IsWaterMark(fileExt) && isWater) { switch (this.siteConfig.watermarktype) { case 1: WaterMark.AddImageSignText(serverFileName, serverFileName, this.siteConfig.watermarktext, this.siteConfig.watermarkposition, this.siteConfig.watermarkimgquality, this.siteConfig.watermarkfont, this.siteConfig.watermarkfontsize); break; case 2: WaterMark.AddImageSignPic(serverFileName, serverFileName, this.siteConfig.watermarkpic, this.siteConfig.watermarkposition, this.siteConfig.watermarkimgquality, this.siteConfig.watermarktransparency); break; } } //如果需要返回原文件名 if (_isReOriginal) { return "{\"msg\": \"1\", \"msgbox\": \"" serverFileName "\", mstitle: \"" originalFileName "\"}"; } return "{\"msg\": \"1\", \"msgbox\": \"" returnFileName "\"}"; } catch { return "{\"msg\": \"0\", \"msgbox\": \"上传过程中发生意外错误!\"}"; } } #region 私有方法 /// <summary> /// 返回上传目录相对路径 /// </summary> /// <param name="fileName">上传文件名</param> private string GetUpLoadPath() { string path = siteConfig.webpath siteConfig.attachpath "/"; //站点目录 上传目录 switch (this.siteConfig.attachsave) { case 1: //按年月日每天一个文件夹 path = DateTime.Now.ToString("yyyyMMdd"); break; default: //按年月/日存入不同的文件夹 path = DateTime.Now.ToString("yyyyMM") "/" DateTime.Now.ToString("dd"); break; } return path "/"; } /// <summary> /// 是否需要打水印 /// </summary> /// <param name="_fileExt">文件扩展名,不含“.”</param> private bool IsWaterMark(string _fileExt) { //判断是否开启水印 if (this.siteConfig.watermarktype > 0) { //判断是否可以打水印的图片类型 ArrayList al = new ArrayList(); al.Add("bmp"); al.Add("jpeg"); al.Add("jpg"); al.Add("png"); if (al.Contains(_fileExt.ToLower())) { return true; } } return false; } /// <summary> /// 是否为图片文件 /// </summary> /// <param name="_fileExt">文件扩展名,不含“.”</param> private bool IsImage(string _fileExt) { ArrayList al = new ArrayList(); al.Add("bmp"); al.Add("jpeg"); al.Add("jpg"); al.Add("gif"); al.Add("png"); if (al.Contains(_fileExt.ToLower())) { return true; } return false; } /// <summary> /// 检查是否为合法的上传文件 /// </summary> private bool CheckFileExt(string _fileExt) { //检查危险文件 string[] excExt = { "asp", "aspx", "php", "jsp", "htm", "html" }; for (int i = 0; i < excExt.Length; i ) { if (excExt[i].ToLower() == _fileExt.ToLower()) { return false; } } //检查合法文件 string[] allowExt = this.siteConfig.attachextension.Split(','); for (int i = 0; i < allowExt.Length; i ) { if (allowExt[i].ToLower() == _fileExt.ToLower()) { return true; } } return false; } /// <summary> /// 检查文件大小是否合法 /// </summary> /// <param name="_fileExt">文件扩展名,不含“.”</param> /// <param name="_fileSize">文件大小(KB)</param> private bool CheckFileSize(string _fileExt, int _fileSize) { //判断是否为图片文件 if (IsImage(_fileExt)) { if (this.siteConfig.attachimgsize > 0 && _fileSize > this.siteConfig.attachimgsize * 1024) { return false; } } else { if (this.siteConfig.attachfilesize > 0 && _fileSize > this.siteConfig.attachfilesize * 1024) { return false; } } return true; } #endregion } }
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论