EXT.NET MVC5 工作随笔08 关于前台上传多个图片和单个图片的写法

来源:互联网 发布:cp1200写频软件下载 编辑:程序博客网 时间:2024/06/01 10:40

后前代码:

 public ActionResult UploadClick(string P_Name, string tName, string ID)//参数为路径 文件全名含格式 要保存到该条数据的ID        {            DirectResult result = new DirectResult();            if (P_Name != "" && tName != "" && ID != "")            {                string tpl = "Uploaded file: {0}<br/>Size: {1} bytes";                if (this.GetCmp<FileUploadField>("FileUploadField1").HasFile)                {                    X.Msg.Show(new MessageBoxConfig                    {                        Buttons = MessageBox.Button.OK,                        Icon = MessageBox.Icon.INFO,                        Title = "Success",                        Message = string.Format(tpl, this.GetCmp<FileUploadField>("FileUploadField1").PostedFile.FileName, this.GetCmp<FileUploadField>("FileUploadField1").PostedFile.ContentLength)                    });                    HttpContextBase context = this.HttpContext;                    try                    {                        HttpPostedFileBase file;                        string filePath = "~/UploadPhoto/" + P_Name + "/";                        if (Directory.Exists(Server.MapPath(filePath)))                        {                            for (int i = 0; i < context.Request.Files.Count; ++i)                            {                                file = context.Request.Files[i];                                if (file == null || file.ContentLength == 0 || string.IsNullOrEmpty(file.FileName)) continue;                                string fileFullName = Server.MapPath(filePath + file.FileName);                                file.SaveAs(fileFullName);                                context.Response.Write("1");                                context.Response.Flush();                            }                        }                        else                        {                            Directory.CreateDirectory(Server.MapPath(filePath));                            for (int i = 0; i < context.Request.Files.Count; ++i)                            {                                file = context.Request.Files[i];                                if (file == null || file.ContentLength == 0 || string.IsNullOrEmpty(file.FileName)) continue;                                string fileFullName = Server.MapPath(filePath + file.FileName);                                file.SaveAs(fileFullName);                                context.Response.Write("1");                                context.Response.Flush();                            }                        }                    }                    catch (Exception ex)                    {                        ReturnMsg.MsgCode = "9999";                        ReturnMsg.Msg = string.Format("系统错误:{0}", ex); context.Response.End();                    }                    finally                    {                        context.Response.End();                    }                }                else                {                    X.Msg.Show(new MessageBoxConfig                    {                        Buttons = MessageBox.Button.OK,                        Icon = MessageBox.Icon.ERROR,                        Title = "Fail",                        Message = "No file uploaded"                    });                }                string URL = "\\UploadPhoto\\" + P_Name + "\\" + tName;                int iCount = BLL_DO_STAMP_FIGURE.Init.InsertURL(ID, URL);//保存进库到表,根据ID                if (iCount > 0)                {                    result.IsUpload = true;                }            }            return result;        }
多张上传时:

JS判断:

 //上传图片判断        var loadFailed = function () {            X.Msg.show({ title: "操作提示", icon: X.MessageBox.ERROR, msg: "文件加载发生错误,请联系维护人员进行解决!", buttons: X.Msg.OK });        };        //上传图片判断        var fileSelected = function (item, file) {            if (file.name == 'image.jpg') {                X.Msg.show({ title: "操作提示", icon: X.MessageBox.ERROR, msg: '不能上传名称为"image.jpg"的文件,请修改!', buttons: X.Msg.OK });                return false;            }            //var TempFileValue = file.name.split(" ");            //if (TempFileValue[2].indexOf("×") < 0) {            //    X.Msg.show({ title: "操作提示", icon: X.MessageBox.INFO, msg: '上传图片名称不规范!', buttons: X.Msg.OK });            //    return false;            //}            //else {            //    var TempZPLXValue = TempFileValue[2].split("×");            //}            //if (TempZPLXValue[1].indexOf(".") < 0) {            //}            //else {            //    var TempZPFDBSValue = TempZPLXValue[1].split(".");            //}            //this.up('grid').store.add({            //    YSYH: App.GP_Main.getSelectionModel().getSelection()[0].data.YSYH,            //    YPBH: App.GP_Main.getSelectionModel().getSelection()[0].data.YPBH,            //    BDZPM: file.name,            //    ZPBH: TempFileValue[0],            //    ZPLRMS: TempFileValue[1],            //    ZPFDBS: TempZPFDBSValue[0]            //});            //App.B_Detail_Upload.enable();        };        //上传图片判断        var fileSelectionError = function (item, file, errorCode, message) {            if (errorCode === SWFUpload.QUEUE_ERROR.QUEUE_LIMIT_EXCEEDED) {                alert("所选文件过多.\n" + (message === 0 ? "单次上传文件个数不得多于100个." : "你可以选择 " + (message > 1 ? "up to " + message + " files." : "一个文件.")));                return;            }            switch (errorCode) {                case SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT:                    alert("错误: 大小超出15M, 文件: " + file.name + ", 文件大小: " + file.size + ", 错误信息: " + message);                    break;                case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:                    alert("错误: 内容为空,不能进行上传, 文件: " + file.name + ", 文件大小: " + file.size + ", 错误信息: " + message);                    break;                case SWFUpload.QUEUE_ERROR.INVALID_FILETYPE:                    alert("错误: 无效的文件类型, 文件: " + file.name + ", 文件大小: " + file.size + ", 错误信息: " + message);                    break;                default:                    alert("错误: " + errorCode + ", 文件: " + file.name + ", 文件大小: " + file.size + ", Message: " + message);                    break;            }        };        //上传图片判断        var uploadError = function (item, file, errorCode, message) {            switch (errorCode) {                case SWFUpload.UPLOAD_ERROR.HTTP_ERROR:                    alert("错误: HTTP 错误, 文件: " + file.name + ", 错误信息: " + message);                    break;                case SWFUpload.UPLOAD_ERROR.UPLOAD_FAILED:                    alert("错误: 上传 错误, 文件: " + file.name + ", 文件大小: " + file.size + ", 错误信息: " + message);                    break;                case SWFUpload.UPLOAD_ERROR.IO_ERROR:                    alert("错误: IO 错误, 文件: " + file.name + ", 错误信息: " + message);                    break;                case SWFUpload.UPLOAD_ERROR.SECURITY_ERROR:                    alert("错误: 安全错误, 文件: " + file.name + ", 错误信息: " + message);                    break;                case SWFUpload.UPLOAD_ERROR.UPLOAD_LIMIT_EXCEEDED:                    alert("错误: 文件超过大小限制, 文件: " + file.name + ", 文件大小: " + file.size + ", 错误信息: " + message);                    break;                case SWFUpload.UPLOAD_ERROR.FILE_VALIDATION_FAILED:                    alert("错误: 文件验证失败, 文件: " + file.name + ", 文件大小: " + file.size + ", 错误信息: " + message);                    break;                default:                    alert("错误: " + errorCode + ", 文件: " + file.name + ", 文件大小: " + file.size + ", 错误信息: " + message);                    break;            }        };        //上传图片判断        var GetUploadResault = function (file, data, response) {            if (response != "1") {                X.Msg.show({ title: "操作提示", icon: X.MessageBox.ERROR, msg: "文件上传发生错误,请联系维护人员进行解决!", buttons: X.Msg.OK });            }        };

<span style="background-color: rgb(240, 240, 240);">                 X.FormPanel().ID("FP_1").Region(Region.Center).Layout(LayoutType.Card).Border(true).Flex(1).Split(true)</span> .TopBar(X.Toolbar()              .Items(                X.FileUploadField().ID("FUF_Photo").Width(400).Icon(Icon.Attach).ButtonText("浏览文件"),
                        X.TextField().ID("PhotoName"),                        X.MultiUpload().ID("MU_Detail").FileDropAnywhere(true).FileSizeLimit("15MB").FileTypes("*.jpg;*.jpeg;*.bmp;")                        .FileTypesDescription("所有文件")                        .FileUploadLimit(100).FileQueueLimit(0).UploadUrl(Url.Action("ProcessRequest"))                        .PostParams(d =>                        {                           d.Add(new Parameter("P_Name", "App.GP_Well.getSelectionModel().getSelection()[0].data.STAMP_FIGURE_NAME", ParameterMode.Raw));                        })                        .Listeners(ls => ls.SwfUploadLoadFailed.Fn = "loadFailed")                        .Listeners(ls => ls.FileSelected.Fn = "fileSelected")                        .Listeners(ls => ls.FileSelectionError.Fn = "fileSelectionError")                        .Listeners(ls => ls.UploadError.Fn = "uploadError")                        .Listeners(ls => ls.UploadSuccess.Fn = "GetUploadResault"),
后台:

                  HttpContextBase context = this.HttpContext;                    try                    {                        HttpPostedFileBase file;                        string filePath = "~/UploadPhoto/" + P_Name + "/";                        if (Directory.Exists(Server.MapPath(filePath)))                        {                            for (int i = 0; i < context.Request.Files.Count; ++i)                            {                                file = context.Request.Files[i];                                if (file == null || file.ContentLength == 0 || string.IsNullOrEmpty(file.FileName)) continue;                                string fileFullName = Server.MapPath(filePath + file.FileName);                                file.SaveAs(fileFullName);                                context.Response.Write("1");                                context.Response.Flush();                            }                        }                        else                        {                            Directory.CreateDirectory(Server.MapPath(filePath));                            for (int i = 0; i < context.Request.Files.Count; ++i)                            {                                file = context.Request.Files[i];                                if (file == null || file.ContentLength == 0 || string.IsNullOrEmpty(file.FileName)) continue;                                string fileFullName = Server.MapPath(filePath + file.FileName);                                file.SaveAs(fileFullName);                                context.Response.Write("1");                                context.Response.Flush();                            }                        }                    }                    catch (Exception ex)                    {                        ReturnMsg.MsgCode = "9999";                        ReturnMsg.Msg = string.Format("系统错误:{0}", ex); context.Response.End();                    }                    finally                    {                        context.Response.End();                    }                }


0 0
原创粉丝点击