<%@ WebHandler Language="C#" Class="Handler" %> using System; using System.Web; using System.IO; public class Handler : IHttpHandler { /// <summary> /// 上传文件夹 /// </summary> private const string UPLOAD_FOLDER = "~/UploadFile/"; public void ProcessRequest(HttpContext context) { int resultVal = (int)ReturnVal.Failed; try { HttpPostedFile myFile = context.Request.Files[0]; if (myFile != null) { if (myFile.InputStream.Length != 0) { string originalFileName = Path.GetFileName(myFile.FileName); //原文件名 string newFileName = string.Format("{0}_{1}", Guid.NewGuid(), originalFileName); //新文件名---组成形式: GUID + 下划线 + 原文件名 string fileAbsPath = context.Server.MapPath(UPLOAD_FOLDER) + newFileName; //绝对路径 myFile.SaveAs(fileAbsPath); resultVal = (int)ReturnVal.Succeed; } else { resultVal = (int)ReturnVal.FileEmpty; } } else { resultVal = (int)ReturnVal.NotSelected; } } catch (Exception) { resultVal = (int)ReturnVal.Failed; } finally { context.Response.Write(resultVal); } } #region## 返回值 /// <summary> /// 返回值 /// </summary> private enum ReturnVal : int { /// <summary> /// 不能上传 0 K大小的文件 /// </summary> FileEmpty = -2, /// <summary> /// 未选中文件 /// </summary> NotSelected = -1, /// <summary> /// 上传失败 /// </summary> Failed = 0, /// <summary> /// 成功 /// </summary> Succeed = 1 } #endregion public bool IsReusable { get { return false; } } }