using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Web; namespace HH.AutoBom.WebUI.Content.js.uploadify { /// /// UploadHanderIMG 的摘要说明 /// public class UploadHanderIMG : IHttpHandler { public void ProcessRequest(HttpContext context) { try { context.Response.ContentType = "text/plain"; context.Response.Charset = "utf-8"; 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 = System.Guid.NewGuid().ToString() + fileExtension;//以GUID为文件重命名保证不会重复 //相对路径文件名称 fileNameLogic = fileLogic + fileRealName; string fileName = strUploadPath + fileRealName; if (fileName != "") { postedFile.SaveAs(fileName); } } context.Response.Write(fileNameLogic); //context.Response.Write(" "); } catch (Exception ex) { context.Response.ContentType = "text/plain"; context.Response.Write(ex.Message); } } #region DateRndName()日期时间+3位数随机 string DateRndName() { Random ra = new Random(); DateTime d = DateTime.Now; string s = null, y, m, dd, h, mm, ss; y = d.Year.ToString(); m = d.Month.ToString(); if (m.Length < 2) m = "0" + m; dd = d.Day.ToString(); if (dd.Length < 2) dd = "0" + dd; h = d.Hour.ToString(); if (h.Length < 2) h = "0" + h; mm = d.Minute.ToString(); if (mm.Length < 2) mm = "0" + mm; ss = d.Second.ToString(); if (ss.Length < 2) ss = "0" + ss; s += y + m + dd + h + mm + ss; s += ra.Next(100, 999).ToString(); return s; } #endregion public bool IsReusable { get { return false; } } } }