/************************************************************************************
* Copyright (C) 2007 supesoft.com,All Rights Reserved *
* File: *
* FileUpLoadCommon.cs *
* Description: *
* 通用上传文件类 *
* Author: *
* Lzppcc *
* Lzppcc@hotmail.com *
* http://www.supesoft.com *
* Finish DateTime: *
* 2007年8月6日 *
* History: *
***********************************************************************************/
* Copyright (C) 2007 supesoft.com,All Rights Reserved *
* File: *
* FileUpLoadCommon.cs *
* Description: *
* 通用上传文件类 *
* Author: *
* Lzppcc *
* Lzppcc@hotmail.com *
* http://www.supesoft.com *
* Finish DateTime: *
* 2007年8月6日 *
* History: *
***********************************************************************************/
- usingSystem;
- usingSystem.Collections.Generic;
- usingSystem.Text;
- usingSystem.Web.UI.WebControls;
- usingSystem.IO;
- usingSystem.Web;
- usingSystem.Drawing;
- namespaceFrameWork
- {
- ///<summary>
- ///通用上传文件类
- ///</summary>
- publicclassFileUpLoadCommon
- {
- #region"PrivateVariables"
- privatestring_path;//上传文件的路径
- privatestring_fileName;//文件名称
- privatestring_newFileName="";//新的文件名
- privatestring_fileName_s;//缩略图文件名称
- privatestring_fileName_sy;//水印图文件名称(文字)
- privatestring_fileName_syp;//水印图文件名称(图片)
- privatestring_webFilePath;//服务器端文件路径
- privatestring_webFilePath_s; //服务器端缩略图路径
- privatestring_webFilePath_sy; //服务器端带水印图路径(文字)
- privatestring_webFilePath_syp; //服务器端带水印图路径(图片)
- privatestring_webFilePath_sypf; //服务器端水印图路径(图片)
- privatestring_errorMsg;//错误信息
- privatebool_isDate=true;//是否加上日期
- privatebool_isThumbnail=true;//是否产生缩图
- privatebool_isWatermarkT=false;//是否产生水印文字
- privatebool_isWatermarkP=false;//是否产生水印叠加图片
- privatebool_isOnly=true;//是否只产生一个文件,原始文件+加水印文字+加水印图片
- //privatebool_isImage=true;//是否只充许图片
- privateint_width=Common.UpImgWidth;//缩图宽度
- privateint_height=Common.UpImgHeight;//缩图高度
- privatestring_mode="Auto";//缩图模式"HW"://指定高宽缩放(可能变形)"W"//指定宽,高按比例"H"//指定高,宽按比例"Cut"://指定高宽裁减(不变形)Auto:自动
- privatestring_addText="Moliyo.com";//水印文字
- privatestring_addPicture=Common.UpLoadDir+"shuiyin.jpg";//水印文字
- privateint_fileSize=0;//图片大小
- privateint_fileWidth=0;//图片宽度
- privateint_fileHeight=0;//图片高度
- privatestring_fileType;//文件类型
- #endregion
- #region属性--旧文件名
- ///<summary>
- ///旧文件名
- ///</summary>
- publicstringfileName
- {
- get
- {
- return_fileName;
- }
- }
- #endregion
- #region属性--上传后新文件名
- ///<summary>
- ///上传后新文件名
- ///</summary>
- publicstringnewFileName
- {
- get
- {
- return_newFileName;
- }
- }
- #endregion
- #region属性--缩图文件名
- ///<summary>
- ///缩图文件名
- ///</summary>
- publicstringfileName_s
- {
- get
- {
- return_fileName_s;
- }
- }
- #endregion
- #region属性--加文字水印的图片
- ///<summary>
- ///加文字水印的图片
- ///</summary>
- publicstringfileName_sy
- {
- get
- {
- return_fileName_sy;
- }
- }
- #endregion
- #region属性--加图片水印的图片
- ///<summary>
- ///加图片水印的图片
- ///</summary>
- publicstringfileName_syp
- {
- get
- {
- return_fileName_syp;
- }
- }
- #endregion
- #region属性--上传的文件的路径
- ///<summary>
- ///上传的文件的路径
- ///</summary>
- publicstringpath
- {
- get
- {
- return_path;
- }
- }
- #endregion
- #region属性--文件大小
- ///<summary>
- ///文件大小
- ///</summary>
- publicintfileSize
- {
- get
- {
- return_fileSize;
- }
- }
- #endregion
- #region属性--图片原始的宽度
- ///<summary>
- ///图片原始的宽度
- ///</summary>
- publicintfileWidth
- {
- get
- {
- return_fileWidth;
- }
- }
- #endregion
- #region属性--图片原始的高度
- ///<summary>
- ///图片原始的高度
- ///</summary>
- publicintfileHeight
- {
- get{return_fileHeight;}
- }
- #endregion
- #region属性--文件的类型
- ///<summary>
- ///文件的类型
- ///</summary>
- publicstringfileType
- {
- get{return_fileType;}
- }
- #endregion
- #region属性--设置水印文字
- ///<summary>
- ///设置水印文字
- ///</summary>
- publicstringaddText
- {
- set{_addText=value;}
- }
- #endregion
- #region属性--设置水印图片
- ///<summary>
- ///设置水印图片
- ///</summary>
- publicstringaddPicture
- {
- set{_addPicture=value;}
- }
- #endregion
- #region构造函数
- ///<summary>
- ///构造函数
- ///</summary>
- publicFileUpLoadCommon()
- :this("/public/",true)
- {
- }
- ///<summary>
- ///构造函数
- ///</summary>
- ///<paramname="filePath">文件路径</param>
- ///<paramname="isDate">是否按日期创建目录</param>
- publicFileUpLoadCommon(stringfilePath,boolisDate)
- {
- _path=filePath;
- _isDate=isDate;
- if(_isDate)
- _path+=DateTime.Now.ToString("yyyyMMdd")+"/";
- stringp=HttpContext.Current.Server.MapPath(_path);
- //如果目录不存在,将创建目录
- if(!Directory.Exists(p))
- Directory.CreateDirectory(p);
- }
- #endregion
- #region方法--保存文件
- ///<summary>
- ///指定缩图的宽高
- ///</summary>
- ///<paramname="fu">文件类型</param>
- ///<paramname="Width">宽</param>
- ///<paramname="Height">高</param>
- ///<returns></returns>
- publicboolSaveImage(FileUploadfu,intWidth,intHeight)
- {
- _width=Width;
- _height=Height;
- returnSaveFile(fu,true);
- }
- ///<summary>
- ///指定缩图的宽高
- ///</summary>
- ///<paramname="fu">文件类型</param>
- ///<paramname="Width">宽</param>
- ///<paramname="Height">高</param>
- ///<paramname="Mode">缩图模式"HW"://指定高宽缩放(可能变形)"W"//指定宽,高按比例"H"//指定高,宽按比例"Cut"://指定高宽裁减(不变形)</param>
- ///<returns></returns>
- publicboolSaveImage(FileUploadfu,intWidth,intHeight,stringMode)
- {
- _width=Width;
- _height=Height;
- _mode=Mode;
- returnSaveFile(fu,true);
- }
- #endregion
- #region方法--保存文件
- ///<summary>
- ///保存文件
- ///</summary>
- ///<paramname="fu">上传文件对象</param>
- ///<paramname="IsImage">是否邮件</param>
- publicboolSaveFile(FileUploadfu,boolIsImage)
- {
- if(fu.HasFile)
- {
- stringfileContentType=fu.PostedFile.ContentType;
- stringname=fu.PostedFile.FileName;//客户端文件路径
- FileInfofile=newFileInfo(name);
- _fileType=fu.PostedFile.ContentType;
- _fileSize=fu.PostedFile.ContentLength;
- boolisfileTypeImages=false;
- if(fileContentType=="image/x-png"||fileContentType=="image/bmp"||fileContentType=="image/gif"||fileContentType=="image/pjpeg")
- {
- isfileTypeImages=true;
- }
- if(IsImage==true&&isfileTypeImages==false)
- {
- _errorMsg="文件类型不是图片!";
- fu.Dispose();
- returnfalse;
- }
- //检测文件扩展名是否正确
- if(!Common.Check_Char_Is(file.Extension.Substring(1).ToLower(),FrameSystemInfo.GetSystemInfoTable.S_SystemConfigData.C_UploadFileExt.ToLower()))
- {
- _errorMsg=string.Format("文件扩展名不符合系统需求:{0}",FrameSystemInfo.GetSystemInfoTable.S_SystemConfigData.C_UploadFileExt);
- fu.Dispose();
- returnfalse;
- }
- if(_fileSize/1024>FrameSystemInfo.GetSystemInfoTable.S_SystemConfigData.C_UploadSizeKb)
- {
- _errorMsg=string.Format("上传文件超过系统允许大小:{0}K",FrameSystemInfo.GetSystemInfoTable.S_SystemConfigData.C_UploadSizeKb);
- fu.Dispose();
- returnfalse;
- }
- _fileName=file.Name;//文件名称
- _newFileName=CreateFileName()+file.Extension;
- _webFilePath=HttpContext.Current.Server.MapPath(_path+_newFileName);//服务器端文件路径
- if(isfileTypeImages)
- {
- _fileName_s="s_"+_newFileName;//缩略图文件名称
- if(_isOnly)
- {
- _fileName_sy=_newFileName;//水印图文件名称(文字)
- _fileName_syp=_newFileName;//水印图文件名称(图片)
- }
- else
- {
- _fileName_sy="sy_"+_newFileName;//水印图文件名称(文字)
- _fileName_syp="syp_"+_newFileName;//水印图文件名称(图片)
- }
- _webFilePath_s=HttpContext.Current.Server.MapPath(_path+_fileName_s); //服务器端缩略图路径
- _webFilePath_sy=HttpContext.Current.Server.MapPath(_path+_fileName_sy); //服务器端带水印图路径(文字)
- _webFilePath_syp=HttpContext.Current.Server.MapPath(_path+_fileName_syp); //服务器端带水印图路径(图片)
- _webFilePath_sypf=HttpContext.Current.Server.MapPath(_addPicture); //服务器端水印图路径(图片)
- //检查文件是否存在
- if(!File.Exists(_webFilePath))
- {
- try
- {
- fu.SaveAs(_webFilePath);//使用SaveAs方法保存文件
- if(_isWatermarkT)
- AddShuiYinWord(_webFilePath,_webFilePath_sy);
- if(_isWatermarkP)
- AddShuiYinPic(_webFilePath,_webFilePath_syp,_webFilePath_sypf);
- if(_isThumbnail)
- MakeThumbnail(_webFilePath,_webFilePath_s,_width,_height,_mode);//生成缩略图方法
- //只有上传完了,才能获取图片大小
- if(File.Exists(_webFilePath))
- {
- System.Drawing.ImageoriginalImage=System.Drawing.Image.FromFile(_webFilePath);
- try
- {
- _fileHeight=originalImage.Height;
- _fileWidth=originalImage.Width;
- }
- finally
- {
- originalImage.Dispose();
- }
- }
- _errorMsg=string.Format("提示:文件“{0}”成功上传,并生成“{1}”缩略图,文件类型为:{2},文件大小为:{3}B",_newFileName,_fileName_s,fu.PostedFile.ContentType,fu.PostedFile.ContentLength);
- fu.Dispose();
- returntrue;
- }
- catch(Exceptionex)
- {
- _errorMsg="提示:文件上传失败,失败原因:"+ex.Message;
- }
- }
- else
- {
- _errorMsg="提示:文件已经存在,请重命名后上传";
- }
- }
- else
- {
- //上传文件
- //检查文件是否存在
- if(!File.Exists(_webFilePath))
- {
- try
- {
- fu.SaveAs(_webFilePath);//使用SaveAs方法保存文件
- _errorMsg=string.Format("提示:文件“{0}”成功上传,并生成“{1}”缩略图,文件类型为:{2},文件大小为:{3}B",_newFileName,_fileName_s,fu.PostedFile.ContentType,fu.PostedFile.ContentLength);
- fu.Dispose();
- returntrue;
- }
- catch(Exceptionex)
- {
- _errorMsg="提示:文件上传失败,失败原因:"+ex.Message;
- }
- }
- else
- _errorMsg="提示:文件已经存在,请重命名后上传";
- }
- }
- fu.Dispose();
- returnfalse;
- }
- #endregion
- #region方法--创建新的文件名
- ///<summary>
- ///创建新的文件名
- ///</summary>
- ///<returns></returns>
- publicstringCreateFileName()
- {
- stringguid=System.Guid.NewGuid().ToString().ToLower();
- guid=guid.Replace("-","");
- returnDateTime.Now.ToString("yyyyMMddhhmmss")+guid.Substring(0,4);
- }
- #endregion
- #region方法--删除文件
- ///<summary>
- ///删除文件
- ///</summary>
- ///<paramname="filename"></param>
- publicstaticvoidDeleteFile(stringfilename)
- {
- strings=HttpContext.Current.Server.MapPath(filename);
- if(File.Exists(s))
- {
- try
- {
- File.Delete(s);
- }
- catch
- {}
- }
- }
- #endregion
- #region方法--生成缩略图
- /**/
- ///<summary>
- ///生成缩略图
- ///</summary>
- ///<paramname="originalImagePath">源图路径(物理路径)</param>
- ///<paramname="thumbnailPath">缩略图路径(物理路径)</param>
- ///<paramname="width">缩略图宽度</param>
- ///<paramname="height">缩略图高度</param>
- ///<paramname="mode">生成缩略图的方式</param>
- publicstaticvoidMakeThumbnail(stringoriginalImagePath,stringthumbnailPath,intwidth,intheight,stringmode)
- {
- System.Drawing.ImageoriginalImage=System.Drawing.Image.FromFile(originalImagePath);
- inttowidth=width;
- inttoheight=height;
- intx=0;
- inty=0;
- intow=originalImage.Width;
- intoh=originalImage.Height;
- if(ow<towidth&&oh<toheight)
- {
- originalImage.Save(thumbnailPath);
- }
- else
- {
- switch(mode.ToUpper())
- {
- case"HW"://指定高宽缩放(可能变形)
- break;
- case"W"://指定宽,高按比例
- toheight=originalImage.Height*width/originalImage.Width;
- break;
- case"H"://指定高,宽按比例
- towidth=originalImage.Width*height/originalImage.Height;
- break;
- case"CUT"://指定高宽裁减(不变形)
- if((double)originalImage.Width/(double)originalImage.Height>(double)towidth/(double)toheight)
- {
- oh=originalImage.Height;
- ow=originalImage.Height*towidth/toheight;
- y=0;
- x=(originalImage.Width-ow)/2;
- }
- else
- {
- ow=originalImage.Width;
- oh=originalImage.Width*height/towidth;
- x=0;
- y=(originalImage.Height-oh)/2;
- }
- break;
- case"AUTO"://自动适应高度
- if(ow>oh)
- {
- //newwidth=200;
- toheight=(int)((double)oh/(double)ow*(double)towidth);
- }
- else
- {
- //newheight=200;
- towidth=(int)((double)ow/(double)oh*(double)toheight);
- }
- break;
- default:
- break;
- }
- //进行缩图
- Bitmapimg=newBitmap(towidth,toheight);
- img.SetResolution(72f,72f);
- Graphicsgdiobj=Graphics.FromImage(img);
- gdiobj.CompositingQuality=System.Drawing.Drawing2D.CompositingQuality.HighQuality;
- gdiobj.SmoothingMode=System.Drawing.Drawing2D.SmoothingMode.HighQuality;
- gdiobj.InterpolationMode=System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
- gdiobj.PixelOffsetMode=System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
- gdiobj.FillRectangle(newSolidBrush(Color.White),0,0,
- towidth,toheight);
- Rectangledestrect=newRectangle(0,0,
- towidth,toheight);
- gdiobj.DrawImage(originalImage,destrect,0,0,ow,
- oh,GraphicsUnit.Pixel);
- System.Drawing.Imaging.EncoderParametersep=newSystem.Drawing.Imaging.EncoderParameters(1);
- ep.Param[0]=newSystem.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality,(long)100);
- System.Drawing.Imaging.ImageCodecInfoici=GetEncoderInfo("image/jpeg");
- try
- {
- if(ici!=null)
- {
- img.Save(thumbnailPath,ici,ep);
- }
- else
- {
- img.Save(thumbnailPath,System.Drawing.Imaging.ImageFormat.Jpeg);
- }
- }
- catch(System.Exceptione)
- {
- throwe;
- }
- finally
- {
- gdiobj.Dispose();
- img.Dispose();
- }
- }
- originalImage.Dispose();
- }
- privatestaticSystem.Drawing.Imaging.ImageCodecInfoGetEncoderInfo(StringmimeType)
- {
- intj;
- System.Drawing.Imaging.ImageCodecInfo[]encoders;
- encoders=System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders();
- for(j=0;j<encoders.Length;++j)
- {
- if(encoders[j].MimeType==mimeType)
- returnencoders[j];
- }
- returnnull;
- }
- #endregion
- #region方法--在图片上增加文字水印
- /**/
- ///<summary>
- ///在图片上增加文字水印
- ///</summary>
- ///<paramname="Path">原服务器图片路径</param>
- ///<paramname="Path_sy">生成的带文字水印的图片路径</param>
- protectedvoidAddShuiYinWord(stringPath,stringPath_sy)
- {
- System.Drawing.Imageimage=System.Drawing.Image.FromFile(Path);
- System.Drawing.Graphicsg=System.Drawing.Graphics.FromImage(image);
- g.DrawImage(image,0,0,image.Width,image.Height);
- System.Drawing.Fontf=newSystem.Drawing.Font("Verdana",16);
- System.Drawing.Brushb=newSystem.Drawing.SolidBrush(System.Drawing.Color.Blue);
- g.DrawString(_addText,f,b,15,15);
- g.Dispose();
- image.Save(Path_sy);
- image.Dispose();
- }
- #endregion
- #region方法--在图片上生成图片水印
- /**/
- ///<summary>
- ///在图片上生成图片水印
- ///</summary>
- ///<paramname="Path">原服务器图片路径</param>
- ///<paramname="Path_syp">生成的带图片水印的图片路径</param>
- ///<paramname="Path_sypf">水印图片路径</param>
- protectedvoidAddShuiYinPic(stringPath,stringPath_syp,stringPath_sypf)
- {
- System.Drawing.Imageimage=System.Drawing.Image.FromFile(Path);
- System.Drawing.ImagecopyImage=System.Drawing.Image.FromFile(Path_sypf);
- System.Drawing.Graphicsg=System.Drawing.Graphics.FromImage(image);
- g.DrawImage(copyImage,newSystem.Drawing.Rectangle(image.Width-copyImage.Width,image.Height-copyImage.Height,copyImage.Width,copyImage.Height),0,0,copyImage.Width,copyImage.Height,System.Drawing.GraphicsUnit.Pixel);
- g.Dispose();
- image.Save(Path_syp);
- image.Dispose();
- }
- #endregion
- }
- }