上传文件-layui+ashx
public void ProcessRequest (HttpContext context) { if (true) { context.Response.ContentType = "text/plain"; result result = new result(); string fileNewName = string.Empty; string filePath = string.Empty; HttpFileCollection files = context.Request.Files; if (files.Count > 0) { //设置文件名 fileNewName = DateTime.Now.ToString("yyyyMMddHHmmssff") + "_" + System.IO.Path.GetFileName(files[0].FileName); //保存文件 files[0].SaveAs(context.Server.MapPath("../Files/" + fileNewName)); result.code = "200"; result.msg = "文件上传成功!"; } else { result.code = "200"; result.msg = "文件上传失败!"; } context.Response.Write(new JavaScriptSerializer().Serialize(result)); context.Response.End(); } } public class result{ public string code { get; set; } public string msg { get; set; } }
//创建上传组件 upload.render({ elem: '#uploadBtn', url: './FileOption.ashx', dataType:"json", accept: 'file', //允许上传的文件类型 size: 0, //最大允许上传的文件大小, data: function () { //请求上传接口的额外参数 }, done: function (res) { if (res.code === "200") { layer.msg(res.msg); } else { layer.msg(res.msg); } }, error: function () { layer.msg("请求异常!"); } });
要么生,要么死