时间:2021-07-01 10:21:17 帮助过:22人阅读
js代码需要先构造一个form表单,把所有数据添加到构造的form中。代码如下:
//添加文章 function AddArticle() { var tmp = {}; tmp.xj_MenuId = $("#ddlCategoryId option:selected").val(); if ($("#PL").prop("checked")) { tmp.is_Msg = 1; } else { tmp.is_Msg = 0; } if ($("#ZD").prop("checked")) { tmp.is_Top = 1; } else { tmp.is_Top = 0; } if ($("#TJ").prop("checked")) { tmp.is_Red = 1; } else { tmp.is_Red = 0; } if ($("#JY").prop("checked")) { tmp.xj_IsLocalNet = 1; } else { tmp.xj_IsLocalNet = 0; } tmp.xj_RelayMenu = ""; $(".artShare_leaveOneList").find(".artShare_Item input").each(function() { if ($(this).prop("checked")) { if (tmp.xj_RelayMenu == "") { tmp.xj_RelayMenu += $(this).val(); } else { tmp.xj_RelayMenu += "," + $(this).val(); } } }); tmp.xj_EduEnable = "" $(".unitlist").find(".artShare_Item input").each(function() { if ($(this).prop("checked")) { if (tmp.xj_EduEnable == "") { tmp.xj_EduEnable += $(this).val(); } else { tmp.xj_EduEnable += "," + $(this).val(); } } }); tmp.xj_Title = $("#txtTitle").val(); tmp.LinkType = $(".artEdit_urlTypeBox").find(".selected").attr("data-id"); if (tmp.LinkType == 0) { tmp.xj_Content = UE.getEditor(‘editor‘).getContent(); if (tmp.xj_Content == "") { jsprint("文章内容不能为空", ‘‘, ‘Error‘); return false; } } if (tmp.LinkType == 1) { tmp.TitleLink = $("#txtCallIndex").val(); if (tmp.TitleLink == "") { jsprint("文章链接不能为空", ‘‘, ‘Error‘); return false; } } tmp.xj_AddTime = $("#txtAddTime").val(); if (tmp.xj_AddTime == "") { jsprint("添加时间不能为空", ‘‘, ‘Error‘); return false; } if (tmp.xj_Title == "") { jsprint("文章标题不能为空", ‘‘, ‘Error‘); return false; } if (tmp.xj_MenuId == 0 || tmp.xj_MenuId == 1) { jsprint("栏目选择不正确(如“├ * * * *“)", ‘‘, ‘Error‘); return false; } var pams = []; pams.push($(‘<input>‘, { name: ‘MenuId‘, value: tmp.xj_MenuId })); pams.push($(‘<input>‘, { name: ‘Msg‘, value: tmp.is_Msg })); pams.push($(‘<input>‘, { name: ‘Title‘, value: tmp.xj_Title })); pams.push($(‘<input>‘, { name: ‘AddTime‘, value: tmp.xj_AddTime })); if (tmp.LinkType == 1) { pams.push($(‘<input>‘, { name: ‘TitleLink‘, value: tmp.TitleLink })); } if (tmp.LinkType == 0) { pams.push($(‘<input>‘, { name: ‘Content‘, value: tmp.xj_Content })); } if (tmp.LinkType == 2) { var file1 = $(".file1").val(); if (file1 == "") { jsprint("文件不能为空", ‘‘, ‘Error‘); return false; } } pams.push($(‘<input>‘, { name: ‘LinkType‘, value: tmp.LinkType })); pams.push($(‘<input>‘, { name: ‘EduEnable‘, value: tmp.xj_EduEnable })); pams.push($(‘<input>‘, { name: ‘RelayMenu‘, value: tmp.xj_RelayMenu })); pams.push($(‘<input>‘, { name: ‘Top‘, value: tmp.is_Top })); pams.push($(‘<input>‘, { name: ‘Red‘, value: tmp.is_Red })); pams.push($(‘<input>‘, { name: ‘IsLocalNet‘, value: tmp.xj_IsLocalNet })); pams.push($(‘<input>‘, { name: ‘op‘, value: "add_edit_article" })); pams.push($(‘<input>‘, { name: ‘state‘, value: "Add" })); pams.push($(‘<input>‘, { name: ‘navName‘, value: navName })); var imgfile = $(".file2").val(); if (imgfile == "") { jsprint("封面图不能为空", ‘‘, ‘Error‘); return false; } var turnForm = document.createElement("form"); $(‘<form>‘, { target: ‘file_upload_return‘, method: ‘post‘, enctype: "multipart/form-data", action: ‘/Ajax/Manage.ashx‘ }).append(pams).append($(".file1")).append($(".file2")).submit(); $("#file_upload_return").load(function() {//获取iframe中的内容 var body = $(window.frames[‘file_upload_return‘].document.body); var data = JSON.parse(body[0].textContent); if (data.status) { jsprint(data.msg, ‘/Admin/School/Article_list.aspx‘, ‘Sucess‘); return false; } jsprint(data.msg, ‘‘, ‘Error‘); return false; }); }
一般处理程序处理数据:
private void Add_Edit_Artice() { string xj_MenuId = cnt.Request["MenuId"]; string is_Msg = cnt.Request["Msg"]; string xj_Title = cnt.Request["Title"]; string xj_AddTime = cnt.Request["AddTime"]; string TitleLink = cnt.Request["TitleLink"]; string xj_Content = cnt.Request["Content"]; string LinkType = cnt.Request["LinkType"]; string xj_EduEnable = cnt.Request["EduEnable"]; string xj_RelayMenu = cnt.Request["RelayMenu"]; string is_Top = cnt.Request["Top"]; string is_Red = cnt.Request["Red"]; string xj_IsLocalNet = cnt.Request["IsLocalNet"]; string state = cnt.Request["state"]; string navName = cnt.Request["navName"]; if (!ChkAdminLevel(navName, state)) { cnt.Response.Write(JsonHelper.SerializeObject(new { status = false, msg = "你无权限操作" })); return; } if (string.IsNullOrEmpty(xj_Title)) { cnt.Response.Write(JsonHelper.SerializeObject(new { status = false, msg = "文章标题不能为空" })); return; } if (string.IsNullOrEmpty(xj_AddTime.ToString())) { cnt.Response.Write(JsonHelper.SerializeObject(new { status = false, msg = "文章添加时间不能为空" })); return; } if (int.Parse(LinkType) == 1) { if (string.IsNullOrEmpty(TitleLink)) { cnt.Response.Write(JsonHelper.SerializeObject(new { status = false, msg = "文章标题链接不能为空" })); return; } } if (int.Parse(LinkType) == 0) { if (string.IsNullOrEmpty(xj_Content)) { cnt.Response.Write(JsonHelper.SerializeObject(new { status = false, msg = "文章内容不能为空" })); return; } string key = ""; if (KeywordsHelper.CheckKeywords(xj_Content, out key)) { cnt.Response.Write(JsonHelper.SerializeObject(new { status = false, msg = "关键词含有非法字符”" + key + "“" })); return; } } try { XinjianSoft.Web.BLL.web_News bll = new XinjianSoft.Web.BLL.web_News(); string relativePath = ""; XinjianSoft.Web.Model.web_News model = new XinjianSoft.Web.Model.web_News(); HttpFileCollection fileCollection = cnt.Request.Files;//获取客户端传来的文件六流 if (state == "Add") { if (int.Parse(is_Msg) == 1) { model.is_Msg = true; } else { model.is_Msg = false; } if (int.Parse(is_Red) == 1) { model.is_Red = true; } else { model.is_Red = false; } if (int.Parse(is_Top) == 1) { model.is_Top = true; } else { model.is_Top = false; } if (int.Parse(xj_IsLocalNet) == 1) { model.xj_IsLocalNet = true; } else { model.xj_IsLocalNet = false; } model.LinkType = int.Parse(LinkType); model.NewsState = "0"; if (LinkType == "1") { model.TitleLink = TitleLink; } model.xj_AddTime = DateTime.Now; model.xj_AddUserId = usermodel.xj_UserName.ToString(); model.xj_Author = usermodel.xj_UserName; if (LinkType == "0") { model.xj_Content = xj_Content; } model.xj_EduEnable = xj_EduEnable; model.xj_Enable = 0; model.xj_Hits = 1; model.xj_id = Guid.NewGuid(); model.xj_MenuId = new Guid(xj_MenuId); model.xj_RelayMenu = xj_RelayMenu; model.xj_Sort = 0; model.xj_Sources = navName; model.xj_Title = xj_Title; model.xj_UnitId = usermodel.UnitID; model.xj_TrendsShow = false; if (fileCollection.Count == 0) { //未接收到文件 //防止发生异常 cnt.Response.Write(JsonHelper.SerializeObject(new { status = false, msg = "你未选中任何图片" })); return; } var file1 = fileCollection["file1"]; var file2 = fileCollection["file2"]; if (file1 != null) { //服务器段相对路径,上传到相册所在的文件夹下 relativePath = "/Upload/ArtcleFile/" + usermodel.xj_UserName + "/" + DateTime.Now.ToString("yyyyMM"); string strpath = UploadImg(fileCollection["file1"], relativePath);//获得文件存储路径 if (strpath == "") { cnt.Response.Write(JsonHelper.SerializeObject(new { status = false, msg = "保存图片发生异常" })); return; } model.TitleLink = strpath; } if (file2 != null) { //服务器段相对路径,上传到相册所在的文件夹下 relativePath = "/Upload/ArtcleImage/" + usermodel.xj_UserName + "/" + DateTime.Now.ToString("yyyyMM"); string strpath = UploadImg(fileCollection["file2"], relativePath);//获得文件存储路径 if (strpath == "") { cnt.Response.Write(JsonHelper.SerializeObject(new { status = false, msg = "保存图片发生异常" })); return; } model.xj_PhotoUrl = strpath; } if (bll.Add(model)) { cnt.Response.Write(JsonHelper.SerializeObject(new { status = true, msg = "保存成功" })); return; } cnt.Response.Write(JsonHelper.SerializeObject(new { status = false, msg = "保存失败" })); return; } } catch (System.Exception ex) { cnt.Response.Write(JsonHelper.SerializeObject(new { status = false, msg = ex.Message })); return; } }
基于file上传文件的并发上传(多个文件一起上传到后台并把数据存储的同一条数据中,如 数据库字段videopath,imge。前台发送来的文件file1,file2。 videopath=file1,imge=file2)
标签:else 选中 服务 min message 前台 pen end help