【Layui__上传】多图上传
2021-02-08 10:17
                         标签:表示   文件信息   each   eal   adf   初始化   png   serve   null    【Layui__上传】多图上传 标签:表示   文件信息   each   eal   adf   初始化   png   serve   null    原文地址:https://www.cnblogs.com/kikyoqiang/p/13081327.html前端
                
                        预览图:
                        
                    
                ‘)
                    var img = new Image();
                    img.src = result;
                    img.onload = function () { //初始化夹在完成后获取上传图片宽高,判断限制上传图片的大小。
                        obj.upload(index, file); //满足条件调用上传方法Q
                        //if (img.width == 343 && img.height == 240) {
                        //    obj.upload(index, file); //满足条件调用上传方法
                        //} else {
                        //    flag = false;
                        //    layer.msg("您上传的小图大小必须是343*240尺寸!");
                        //    return false;
                        //}
                    }
                    return flag;
                });
            }
            , before: function (obj) {
                layer.load();
                this.data = { "exhibitorId": $("#ExhibitorId2").val() }//携带额外的数据
                //预读本地文件示例,不支持ie8
                //obj.preview(function (index, file, result) {
                //    $(‘#previewId‘).append(‘
‘)
                //});
            }
            , done: function (data) {
                console.log(data);
                layer.closeAll(‘loading‘);
                if (data.result == 1) {
                    var imgUrl = $("#ImgUrl").val();
                    $("#ImgUrl").val(imgUrl + "," + data.data.filename);
                    layer.msg(data.msg, { icon: 1, time: 2000 });
                    $("#previewId").find("img").each(function () {
                        $(this).css("max-width", "700px");
                        $(this).css("margin-top", "10px");
                    });
                }
                else {
                    layer.msg(data.msg, { icon: 2, time: 2000 });
                }
            }
        });
后台
        public JsonResult UploadResult()
        {
            string exhibitorId = Request.Params["exhibitorId"];
            string filename = "";
            bool isOk = false;
            foreach (string uploadFile in Request.Files)
            {
                HttpPostedFileBase file = Request.Files[uploadFile] as HttpPostedFileBase;
                foreach (string item in System.Web.HttpContext.Current.Application["AllowPicType"].ToString().Replace("‘", "").Replace(" ", "").Split(‘,‘))
                {
                    if (file.FileName.EndsWith(item, true, null))
                    {
                        string path = "/exhibitoupload/" + exhibitorId + "/result/";
                        if (!Directory.Exists(Server.MapPath(path)))
                        {
                            Directory.CreateDirectory(Server.MapPath(path));
                        }
                        //filename = path + DateTime.Now.ToString("yyyyMMddHHmmss") + new Random().Next(1000, 9999) + "." + item;
                        filename = path + Guid.NewGuid() + "." + item;
                        file.SaveAs(Server.MapPath(filename));
                        isOk = true;
                    }
                }
            }
            AjaxResult ajaxResult = new AjaxResult();
            if (isOk)
            {
                ajaxResult.result = 1;
                ajaxResult.msg = "上传成功";
            }
            else
            {
                ajaxResult.result = 0;
                ajaxResult.msg = "上传失败";
            }
            ajaxResult.data.Add("filename", filename);
            return Json(ajaxResult, JsonRequestBehavior.AllowGet);
        }