上传文件-layui+ashx

 

一、首先了解一下layui关于这个组件吧

文档说明:https://www.layui.com/doc/modules/upload.html
demo:https://www.layui.com/demo/upload.html

二、在我们项目中的应用

eaurl是往后台传参,是文件的存入服务器的路径,成功后会返回文件名,再将文件名存入库中

复制代码
eaUrl = "~" + eaUrl.substring(1);
                //initUploadify("uploadify", "fileQueue");
                layui.use('upload', function () {
                    var $ = layui.jquery
                    , upload = layui.upload;

                    upload.render({
                        elem: '#filebtn'
                        , url: '../LayuiUploadHandler.ashx' //上传接口

                        , accept: 'file' //普通文件
                        , data: { folder: eaUrl }
                        , done: function (res) {
                            if (res.msg != "")
                            {
                                alert(res.msg);
                                layer.msg('上传成功');
                                $j("#<%=hfFiles.ClientID %>").val(res.msg);
                                //ShowFiles($j("#fileDiv2"), queueID, eaUrl + new Date().getFullYear().toString());
                            }
                            
                        },error: function(index, upload){
                            
                            layer.msg('上传出错');
                        
                        }

                    });
                })
复制代码
复制代码
public class LayuiUploadHandler : IHttpHandler
    {

     
           public void ProcessRequest(HttpContext context)
        {
            string newFileName = string.Empty;
            try
            {
                result ret = new result();
                OilDigital.CGGL.BLL.LogService.LogOperationString("上传开始:");
                context.Response.ContentType = "text/plain";
                context.Response.Charset = "utf-8";

                HttpPostedFile file = HttpContext.Current.Request.Files[0];
                string uploadPath =
                    HttpContext.Current.Server.MapPath(@context.Request["folder"]);
                
                if (file != null)
                {
                    if (!Directory.Exists(uploadPath))
                    {
                        Directory.CreateDirectory(uploadPath);
                    }
                    newFileName = file.FileName;
                    if (newFileName.LastIndexOf("\\") > -1)
                    {
                        newFileName = DateTime.Now.Ticks + "_" + newFileName.Substring(newFileName.LastIndexOf("\\") + 1);
                    }
                    else
                    {
                        newFileName = DateTime.Now.Ticks + "_" + newFileName;
                    }
                     
                    file.SaveAs(uploadPath + newFileName);
                    //下面这句代码缺少的话,上传成功后上传队列的显示不会自动消失
                    //context.Response.Write(newFileName);
                    ret.msg = newFileName;
                    OilDigital.CGGL.BLL.LogService.LogOperationString("上传完成:"+ newFileName);
                }
                else
                {
                    ret.msg = "上传文件失败";
                    //context.Response.Write("");
                }
                context.Response.Write(new JavaScriptSerializer().Serialize(ret));
                context.Response.End();


            }
            catch (Exception ex)
            {

                throw new Exception("导入文件出错:" +ex.Message);
            }  
        }
        

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
    public class result
    {
        public result()
        {
            this.code = "200";
        }
        public string code { get; set; }
        public string msg { get; set; }
    }
复制代码

  

 

打广告:有需要微信投票、点赞、注册的朋友可以找我哦:18963948278

posted @   爱生活,爱代码  阅读(415)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示