/// <summary>删除单一文件,并删除数据库记录</summary> /// <param name="picPath">/upload/1.jpg</param> /// <returns></returns> public void Upload_OneDelPic(string picPath) { if (string.IsNullOrEmpty(picPath)) { return; } if (picPath.Length < 4) { return; } DirFileHelper.DelPicFile(picPath); Upload_DelRs(picPath); }
/// <summary>比较_oldFile 同 _newFile,删除旧文件</summary> /// <param name="oldFile">旧文件</param> /// <param name="newFile">新文件</param> /// <param name="isPic">是图片文件,同时删除小图片</param> public void Upload_DiffFile(string oldFile, string newFile, bool isPic) { //删除旧的文件 if (oldFile.Length > 4 && string.Compare(oldFile, newFile, true) != 0) { if (isPic) { DirFileHelper.DelPicFile(oldFile); } else { DirFileHelper.DeleteFile(oldFile); } Upload_DelRs(oldFile); } }
/// <summary>批量删除,根据JoinName和JoinId</summary> /// <param name="joinName">相关表名,即UploadConfig.JoinName</param> /// <param name="joinId">相关ID,即UploadConfig.JoinID</param> public static void Upload_BatDelPic(string joinName, int joinId) { if (string.IsNullOrEmpty(joinName) || joinId < 1) { return; } //string sql = "select Id,Path from UploadFile where JoinName='+joinName+' and JoinId=" + joinId; //------------------------------------------------- var dt = UploadFile.Find(x => x.JoinId == joinId && x.JoinName == joinName); foreach (var rs in dt) { DirFileHelper.DelPicFile(rs.Path); UploadFile.Delete(x => x.Id == rs.Id); } }
/// <summary>FCKEditor 使用,整理_UpList,把不存在的文件删除</summary> /// <param name="sHtml">Html代码</param> /// <param name="upList">UpFileList列表</param> /// <returns></returns> public string FCK_BatchDelPic(string sHtml, string upList) { if (upList.Length == 0) { return(""); } const string basePath = "/UploadFile/"; string ss = ""; int tj = basePath.Length + 3; upList = upList.Replace("|", ","); upList = upList.Replace(",,", ","); string[] aFiles = StringHelper.SplitMulti(upList, ","); var sb = new StringBuilder(); foreach (string c in aFiles) { if (c.Length > tj && StringHelper.IsRndFileName(c)) { if (sHtml.IndexOf(c) > 0) { sb.Append(c); sb.Append(","); } else { ss = c.Substring(0, basePath.Length); if (string.Compare(ss, basePath, true) == 0) { DirFileHelper.DelPicFile(c); Upload_DelRs(c); } } } } ss = sb.ToString(); if (ss.Length > 0) { ss = StringHelper.DelStrSign(ss, ","); } return(ss); }
/// <summary>上传文件,并按设置生成缩略图,水印</summary> /// <returns></returns> public bool UploadFile(HttpPostedFile oFile = null) { #region 检查设置 if (!this.IsEnabled) { SendResponse(500, ""); return(false); } if (this.IsChkSrcPost) { if (!RequestHelper.ChkSrcPost()) { SendResponse(501, ""); return(false); } } if (this._savePath.Length < 1) { SendResponse(101, "SavePath未设置"); return(false); } if (oFile == null) { if (this.FilePostName.Length < 1) { SendResponse(101, "filePostName未设置"); return(false); } } this._savePath = DirFileHelper.FixDirPath(_savePath) + DateTime.Now.ToString("yyMM") + "/"; bool isOk = DirFileHelper.CheckSaveDir(this._savePath); if (!isOk) { SendResponse(101, "SavePath设置不当:" + this._savePath + ", 或权限不足!"); return(false); } #endregion //------------------------------------------------ #region 获取文件对象 //获取文件对象 if (oFile == null) { oFile = HttpContext.Current.Request.Files[FilePostName]; } if (oFile == null) { SendResponse(201, ""); return(false); } //------------------------------------------------ //原始文件名; this.SrcName = Path.GetFileName(oFile.FileName); #endregion //------------------------------------------------ #region 检查文件大小 this._fileSize = oFile.ContentLength; //不能上传小于10字节的内容 if (this.SrcName.Length < 3 || this._fileSize < 10) { SendResponse(201, ""); return(false); } //检测文件大小是否超过限制 if (this._fileSize > (this._maxSize * 1024)) { SendResponse(301, ""); return(false); } #endregion //------------------------------------------------ #region 检查文件类型 this.FileExt = Path.GetExtension(this.SrcName).ToLower().TrimStart('.'); if (!checkAllowedExt(this.FileExt)) { SendResponse(202, ""); return(false); } #endregion #region 文件 //上传 string sServerDir = _savePath; if (sServerDir.IndexOf(":") < 0) { sServerDir = DirFileHelper.FixDirPath(DirFileHelper.GetMapPath(sServerDir)); } string sNewFile = ""; //新文件名(系统生成) string sNewRoot = ""; //新文件路径(绝对路径) while (true) { sNewFile = DirFileHelper.GetRndFileName("." + this.FileExt); sNewRoot = System.IO.Path.Combine(sServerDir, sNewFile); if (!DirFileHelper.IsExistFile(sNewRoot)) { try { oFile.SaveAs(sNewRoot); } catch { SendResponse(204, ""); return(false); } break; } } this.NewFile = sNewFile; this.NewPath = _savePath + sNewFile; #endregion //------------------------------------------------ #region 生成缩略图 + 水印 if (this.FileExt == "jpg" || this.FileExt == "png" || this.FileExt == "gif" || this.FileExt == "jpeg" || this.FileExt == "bmp") { #region 取得原始尺寸 try//能取得图片宽高,是真实的图片 { System.Drawing.Image testImage1 = System.Drawing.Image.FromFile(sNewRoot); this.SrcWidth = testImage1.Width; this.SrcHeight = testImage1.Height; testImage1.Dispose(); this.NewWidth = this.SrcWidth; this.NewHeight = this.SrcHeight; } catch { //非法格式,不是真正的图片 DirFileHelper.DeleteFile(sNewRoot); SendResponse(202, ""); return(false); } //------------------------------------------------ //先备份原始图 var tmpPath = ""; tmpPath = System.IO.Path.Combine(sServerDir, DirFileHelper.GetFileNamePostfix(sNewFile, "o")); DirFileHelper.CopyFile(sNewRoot, tmpPath); #endregion if (this._isFixPic || this._isBigPic || this._isMidPic || this._isMinPic) { #region 创建大图 if (this._isBigPic) { tmpPath = System.IO.Path.Combine(sServerDir, DirFileHelper.GetFileNamePostfix(sNewFile, "b")); if (this._bigWidth > 0 && this._bigHeight > 0) { //缩略 MakeThumbImage(sNewRoot, tmpPath, this._bigWidth, this._bigHeight, this._bigQuality, this.CutType); } else//因为不限制宽高,所以直接复制出来就行了 { DirFileHelper.CopyFile(sNewRoot, tmpPath); } //添加水印 if (this.IsWaterPic) { MakeWaterPic(tmpPath); } } #endregion //------------------------------------------------ #region 创建中图 if (this._isMidPic) { tmpPath = System.IO.Path.Combine(sServerDir, DirFileHelper.GetFileNamePostfix(sNewFile, "m")); if (this._midWidth > 0 && this._midHeight > 0) { //缩略图 MakeThumbImage(sNewRoot, tmpPath, this._midWidth, this._midHeight, this._midQuality, this.CutType); } else//因为不限制宽高,所以直接复制出来就行了 { DirFileHelper.CopyFile(sNewRoot, tmpPath); } //添加水印 if (this.IsWaterPic) { MakeWaterPic(tmpPath); } } #endregion //------------------------------------------------ #region 创建小图 if (this._isMinPic) { tmpPath = System.IO.Path.Combine(sServerDir, DirFileHelper.GetFileNamePostfix(sNewFile, "s")); if (this._minWidth > 0 && this._minHeight > 0) { //缩略图 MakeThumbImage(sNewRoot, tmpPath, this._minWidth, this._minHeight, this._minQuality, this.CutType); } else//因为不限制宽高,所以直接复制出来就行了 { DirFileHelper.CopyFile(sNewRoot, tmpPath); } //微型图不用加水印 } #endregion //------------------------------------------------ #region 创建推荐图 if (this._isHotPic) { tmpPath = System.IO.Path.Combine(sServerDir, DirFileHelper.GetFileNamePostfix(sNewFile, "h")); if (this._hotWidth > 0 && this._hotHeight > 0) { //缩略图 MakeThumbImage(sNewRoot, tmpPath, this._hotWidth, this._hotHeight, this._hotQuality, this.CutType); } else//因为不限制宽高,所以直接复制出来就行了 { DirFileHelper.CopyFile(sNewRoot, tmpPath); } //添加水印 if (this.IsWaterPic) { MakeWaterPic(tmpPath); } } #endregion //------------------------------------------------ #region 限制列表图 if (this._isFixPic && this._picWidth > 0 && this._picHeight > 0) { MakeThumbImage(sNewRoot, sNewRoot, this._picWidth, this._picHeight, this._picQuality, this.CutType); } #endregion #region 取得缩放后的图片宽高 try { System.Drawing.Image testImage2 = System.Drawing.Image.FromFile(sNewRoot); this.NewWidth = testImage2.Width; this.NewHeight = testImage2.Height; testImage2.Dispose(); } catch { DirFileHelper.DelPicFile(this.NewPath); SendResponse(202, ""); return(false); } #endregion } //列表图是否加水印 if (DirFileHelper.IsExistFile(sNewRoot) && this.IsWaterPic) { MakeWaterPic(sNewRoot); } } #endregion //上传成功!! return(true); }