layui+ashx文件上传

本文介绍了一个简单的文件上传处理方法,通过设定文件名并保存文件到指定路径。使用了HttpContext上下文来处理HTTP请求,并通过JavaScriptSerializer将结果序列化为JSON格式返回。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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("请求异常!"); 
            }
        });

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值