/// <summary> /// 上传附件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnUpload_Click(object sender, EventArgs e) { HttpPostedFile postedFile = FileUpload1.PostedFile; string uploadPath = ConfigHelper.SitePath + "upfiles/" + DateTime.Now.ToString("yyyyMM") + "/"; //文件保存相对路径 string saveDirectory = Server.MapPath(uploadPath); //文件保存绝对文件夹 // string waterPath = Server.MapPath("../common/images/watermark.gif");//待改 string fileName = Path.GetFileName(postedFile.FileName); //文件名 fileName = fileName.Replace(" ", ""); fileName = fileName.Replace("%", ""); fileName = fileName.Replace("&", ""); fileName = fileName.Replace("#", ""); fileName = fileName.Replace("'", ""); fileName = fileName.Replace("+", ""); string fileExtension = Path.GetExtension(postedFile.FileName); //文件后缀 string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fileName); //没有后缀的文件名 int type = StringHelper.StrToInt(rblistType.SelectedValue, 1); if (postedFile.ContentLength == 0) { ShowError("请选择要上传的文件!"); return; } string[] fileExts = AllowFileExtension.Split(','); bool allow = false; foreach (string str in fileExts) { if (("." + str) == fileExtension) { allow = true; break; } } if (allow == false) { ShowError("您上传的文件格式不被允许!"); return; } if (!Directory.Exists(saveDirectory)) { Directory.CreateDirectory(saveDirectory); } int iCounter = 0; int result = 1; while (true) { string fileSavePath = saveDirectory + fileName; string fileSavePath2 = saveDirectory + "da563457-1c3c-4b28-bf73-92f87f930896" + fileName; if (File.Exists(fileSavePath)) { if (type == 1)//跳过 { result = 1; break; } else if (type == 2)//重命名 { iCounter++; fileName = fileNameWithoutExtension + "(" + iCounter + ")" + fileExtension; } else if (type == 3)//覆盖 { File.Delete(fileSavePath); } } else { if ((fileExtension == ".gif" || fileExtension == ".jpg" || fileExtension == ".jpeg" || fileExtension == ".bmp" || fileExtension == ".png") && chkWatermark.Checked) { postedFile.SaveAs(fileSavePath2); string newFileName = Path.GetFileNameWithoutExtension(postedFile.FileName) + "w(" + iCounter + ")" + Path.GetExtension(postedFile.FileName); string newImagePath = Server.MapPath(uploadPath + newFileName); string waterImagePath = Server.MapPath(ConfigHelper.SitePath + "common/images/watermark/" + SettingManager.GetSetting().WatermarkImage); if (SettingManager.GetSetting().WatermarkType == 2 && File.Exists(waterImagePath)) { Watermark.CreateWaterImage(fileSavePath2, fileSavePath, SettingManager.GetSetting().WatermarkPosition, waterImagePath, SettingManager.GetSetting().WatermarkTransparency, SettingManager.GetSetting().WatermarkQuality); } else { Watermark.CreateWaterText(fileSavePath2, fileSavePath, SettingManager.GetSetting().WatermarkPosition, SettingManager.GetSetting().WatermarkText, SettingManager.GetSetting().WatermarkQuality, SettingManager.GetSetting().WatermarkFontName, SettingManager.GetSetting().WatermarkFontSize); } File.Delete(fileSavePath2); } else { postedFile.SaveAs(fileSavePath); } result = 2; break; } } Response.Redirect(FileName + "?path=" + uploadPath + "&result=" + result); }