using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Web; namespace IFrame.Content.js.uploadify { /// /// UploadHandlerWaterMark 的摘要说明 /// public class UploadHandlerWaterMark : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; try { string fileLogic = context.Request.QueryString["folder"] + "/"; string strUploadPath = context.Request.PhysicalApplicationPath + context.Request.QueryString["folder"].Trim('/').Replace("/", "\\") + "\\"; //创建路径 if (!Directory.Exists(strUploadPath)) { Directory.CreateDirectory(strUploadPath); } string fileNameLogic = string.Empty; for (int i = 0; i < context.Request.Files.Count; i++) { HttpPostedFile postedFile = context.Request.Files[i]; //获取扩展名 string fileExtension = System.IO.Path.GetExtension(postedFile.FileName).ToLower(); //string fileName = strUploadPath + Path.GetFileName(postedFile.FileName); //生成随机名称 string fileRealName = "watermark" + fileExtension;//以GUID为文件重命名保证不会重复 //相对路径文件名称 fileNameLogic = fileLogic + fileRealName; string fileName = strUploadPath + fileRealName; //删除之前的水印文件 if (File.Exists(fileName)) { File.Delete(fileName); } if (fileName != "") { postedFile.SaveAs(fileName); } } context.Response.Write(fileNameLogic); // context.Response.End(); } catch (Exception ex) { context.Response.ContentType = "text/plain"; context.Response.Write(ex.Message); } } public bool IsReusable { get { return false; } } } }