public void Upload(FormCollection collection) { var Utils = new Utils.Utils(); try { if (Request.Files.Count > 0 && Request.Files["file"] != null && Request.Files["file"].ContentLength > 128) { var file_ext = Request.Files["file"].FileName.Split('.').Reverse().First(); /// 文件后缀名过滤 var flage = false; foreach (var item in Utils.GetAppSetting("AllowFiles", typeof(string)).ToString().Split(',')) { if (item.Equals(file_ext)) { flage = true; } } if (!flage) { throw new Exception("禁止上传该类型的文件!当前上传的文件类型为:" + file_ext); } var upload_dir = Server.MapPath("~/Uploads/" + DateTime.Now.ToString("yyyy_MM") + "/"); var now = DateTime.Now.ToString("_yyyy_MM_dd_HHmmss"); var filename = Session["login_name"].ToString() + now + '.' + file_ext; var FileInfo = new System.IO.DirectoryInfo(upload_dir); if (!FileInfo.Exists) { FileInfo.Create(); } Request.Files["file"].SaveAs(upload_dir + filename); var msg = new { code = 0, msg = "文件上传成功", data = new { src = "/Uploads/" + DateTime.Now.ToString("yyyy_MM") + "/" + filename, title = Request.Files["file"].FileName, files = Utils.ToJson(Request.Files.AllKeys), } }; Response.Write(Utils.ToJson(msg)); } else { throw new Exception("请求参数不正确"); } } catch (Exception ex) { var error = new { code = -1, msg = "发生错误:" + ex.Message, data = new { src = "", title = "", } }; Response.Write(Utils.ToJson(error)); } Response.End(); }