public string UploadFileByManager(string strKey) { string strUploadErr = string.Empty; AccountInfo account = BLL.Account.GetLoginAccount(); return UploadFile(strKey, -1, UserType.Manager, (account == null ? "未登录账户" : account.AccountName), ref strUploadErr); } /// /// 管理员上传文件 /// /// /// public string UploadFileByManager(HttpPostedFile postFile) { string strUploadErr = string.Empty; AccountInfo account = BLL.Account.GetLoginAccount(); return UploadFile(postFile, -1, UserType.Manager, (account == null ? "未登录账户" : account.AccountName), ref strUploadErr); } public string UploadFileByUser(string strKey) { return UploadFileByUser(HttpContext.Current.Request.Files[strKey]); } public string UploadFileByUser(HttpPostedFile postFile) { string strUploadErr = string.Empty; return UploadFileByUser(postFile, LoginUser, ref strUploadErr); } /// /// 会员上传文件 /// /// /// /// /// public string UploadFileByUser(HttpPostedFile postFile, UserInfo user, ref string strUploadErr) { int userMaxSpace = user.FileSpace; //会员总容量 int userHasUpload = BLL.FileUpload.GetUserUpTotal(user.UserName); //已上传容量 if (userMaxSpace < userHasUpload + postFile.ContentLength) strUploadErr = "会员文件容量不足"; else { string strFileName = UploadFile(postFile, BLL.Folder.CreateUserFolder(), UserType.User, user.UserName, ref strUploadErr); if (!string.IsNullOrEmpty(strFileName)) return strFileName; } return string.Empty; } public string UploadFile(string strKey, UserType userType, string userName, ref string strUploadErr) { return UploadFile(HttpContext.Current.Request.Files[strKey], -1, userType, UserName, ref strUploadErr); } /// /// 上传文件未归档 /// /// /// /// /// /// public string UploadFile(HttpPostedFile postFile, UserType userType, string userName, ref string strUploadErr) { return UploadFile(postFile, -1, userType, userName, ref strUploadErr); } public string UploadFile(string strKey, int folderID, UserType userType, string userName, ref string strUploadErr) { return UploadFile(HttpContext.Current.Request.Files[strKey], folderID, userType, UserName, ref strUploadErr); } /// /// 上传文件,返回上传后文件的完整虚拟路径 /// /// 文件对象 /// 文件夹ID /// 上传用户类型 /// 上传用户名称 /// 返回的报错信息 /// public string UploadFile(HttpPostedFile postFile, int folderID, UserType userType, string userName, ref string strUploadErr) { string strFullName = string.Empty; //需要返回的文件路径,相对路径 目录+文件名 if (!config.EnableFileUpload) strUploadErr = "系统设置不允许文件上传"; else if (postFile == null || (postFile != null && postFile.ContentLength == 0)) strUploadErr = "无效的文件"; else if (!config.EnableUploadExt.Split('|').Contains(Path.GetExtension(postFile.FileName).ToLower())) strUploadErr = "不支持此文件格式上传"; else { #region 上传文件 string strBaseName = string.Empty; //FileUtils.GetFileBaseName(); //是否有文件名的配置 //默认配置 ${year}${month}${day}${millisecond}${rnd} if (!string.IsNullOrEmpty(config.UploadSaveRule)) strBaseName = StringUtils.GenerateSN(config.UploadSaveRule); //生成新的文件名 else strBaseName = Path.GetFileNameWithoutExtension(postFile.FileName); //原来的文件名 string strExtend = Path.GetExtension(postFile.FileName).ToLower(); //扩展文件名 string strNewFileName = strBaseName + strExtend; string strWaterMarkName = string.Empty; //水印图名称 strFullName = Path.Combine(UploadFolder, strNewFileName); string strThumbName = string.Empty; //缩略图名称 string strPhysicalPath = Server.MapPath(strFullName); int filesize = postFile.ContentLength; if (File.Exists(strPhysicalPath)) { strUploadErr = "文件已经存在"; return string.Empty; } postFile.SaveAs(Server.MapPath(strFullName)); if (ValidateUtils.IsImage(strExtend)) { #region 生成图片缩略图、水印 if (config.WaterMarkPosition > 0) //是否启用水印 { strWaterMarkName = strBaseName + "_watermark" + strExtend; if (config.WaterMarkType.Equals("文字水印") && config.WaterMarkText.Trim().Length > 0) ImageUtils.AddTextWatermark(strPhysicalPath, config.WaterMarkPosition, config.WaterMarkText, config.WaterMarkTextSize, config.WaterMarkTextColor, config.WaterMarkTextFont, (float)config.WaterMarkAlpha); else if (config.WaterMarkType.Equals("图片水印") && File.Exists(Server.MapPath(config.WaterMarkImage))) ImageUtils.AddImageWatermark(strPhysicalPath, config.WaterMarkPosition, Server.MapPath(config.WaterMarkImage), (float)config.WaterMarkAlpha); if (File.Exists(Server.MapPath(Path.Combine(UploadFolder, strWaterMarkName)))) { File.Delete(strPhysicalPath); //删除原图,用水印图替换 strNewFileName = strWaterMarkName; //重新赋值文件名 strFullName = Path.Combine(UploadFolder, strWaterMarkName); //重新赋值全路径 strPhysicalPath = Server.MapPath(strFullName); //这里是水印图的物理路径 } } strThumbName = strFullName.Replace(strExtend, "_thumb" + strExtend); int ThumbSize_Width = ConfigProvider.Configs.ThumbSize.Split('X').Length > 0 ? WebUtils.GetInt(ConfigProvider.Configs.ThumbSize.Split('X')[0]) : 100; int ThumbSize_Height = ConfigProvider.Configs.ThumbSize.Split('X').Length > 1 ? WebUtils.GetInt(ConfigProvider.Configs.ThumbSize.Split('X')[1]) : 100; string strThumbMode = config.ThumbModel; if (string.IsNullOrEmpty(strThumbMode)) strThumbMode = "Cut"; //生成缩略图 ImageUtils.MakeThumbPhoto(strPhysicalPath, base.Server.MapPath(strThumbName), ThumbSize_Width, ThumbSize_Height, strThumbMode); #endregion } FileUploadInfo model = new FileUploadInfo { FolderID = folderID, //文件夹ID -1表示未归类 FileName = strNewFileName, FileExt = strExtend, FileSize = filesize, VirtualPath = strFullName, Thumb = ValidateUtils.IsImage(strExtend) ? strThumbName : string.Empty, //只有图片才有缩略图 OriginalPath = postFile.FileName, //原文件 UserType = userType.ToString(), UserName = userName, DownloadCount = 0, //下载次数 Lang = cultureLang, AutoTimeStamp = System.DateTime.Now }; if (SinGooCMS.BLL.FileUpload.Add(model) > 0) log.AddEvent(userName, userName + "[" + userType + "]上传文件[" + strFullName + "]成功"); #endregion } return strFullName; //全虚拟路径,目录+文件名 }