<%@ WebHandler Language="C#" Class="UploadFile_m" %> using System; using System.Collections.Generic; using System.Web; using System.Data; using System.Data.Common; using System.Web.SessionState; using System.Text; using eMIS; using System.Collections; using System.IO; public class UploadFile_m : IHttpHandler, IRequiresSessionState { public void ProcessRequest (HttpContext context) { if (context.Session["userid"] == null) { LoginManager lm = new LoginManager(context); lm.Login("400002"); //if (!lm.VerifyIdentity()) //{ // context.Response.Write("会话丢失,请重新登陆!"); // context.Response.End(); //} } byte[] byteParsed = null; string filepath = ""; string filename = ""; try { int vals = context.Request.TotalBytes; byte[] bytes = context.Request.BinaryRead(vals); string strContent = System.Text.Encoding.UTF8.GetString(bytes); string[] stringCodes = strContent.Split('|'); byteParsed = new byte[stringCodes.Length]; for (int stringCodeCount = 0; stringCodeCount < stringCodes.Length; stringCodeCount++) { byteParsed[stringCodeCount] = Convert.ToByte(stringCodes[stringCodeCount]); } string wwwroot = context.Request.ApplicationPath; if (wwwroot == "/") wwwroot = ""; filename = context.Request.Params["filename"].ToString() + "." + Guid.NewGuid().ToString(); filepath = context.Request.Params["filepath"].ToString(); if (filepath == "undefined") { filepath = "/app_files/defult/"; } filepath = wwwroot + filepath; //路径 string toFileFullPath = context.Server.MapPath(filepath); //检查是否有该路径 没有就创建 if (!Directory.Exists(toFileFullPath)) { Directory.CreateDirectory(toFileFullPath); } toFileFullPath += filename;//物理路径 FileStream fs = new FileStream(toFileFullPath, FileMode.Create, FileAccess.Write); BinaryWriter bw = new BinaryWriter(fs); bw.Write(byteParsed); bw.Close(); fs.Close(); context.Response.Write("ok!" + filepath + "/" + filename); } catch (Exception e) { context.Response.Write(e.ToString()); return; } } public bool IsReusable { get { return false; } } }