基于file上传文件的并发上传(多个文件一起上传到后台并把数据存储的同一条数据中,如 数据库字段videopath,imge。前台发送来的文件file1,file2。 videopath=file1,
Posted 想飞
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于file上传文件的并发上传(多个文件一起上传到后台并把数据存储的同一条数据中,如 数据库字段videopath,imge。前台发送来的文件file1,file2。 videopath=file1,相关的知识,希望对你有一定的参考价值。
前台代码:
<div class="tab-content"> <dl> <dt>所属栏目</dt> <dd> <div class="rule-single-select"> <select id="ddlCategoryId"> <option value="0">所有栏目</option> </select> </div> </dd> </dl> <dl> <dt>推荐类型</dt> <dd> <div class="rule-multi-checkbox"> <span id="checkboxs"> <input type="checkbox" class="checked" id="PL" /><label>允许评论</label><input type="checkbox" class="checked" id="ZD" /><label>置顶</label><input type="checkbox" class="checked" id="TJ" /><label>推荐</label><input type="checkbox" class="checked" id="JY" /><label>只限局域网访问</label></span> </div> </dd> </dl> <dl> <dt>文章标题</dt> <dd> <asp:TextBox ID="txtTitle" runat="server" CssClass="input normal" datatype="*2-100" sucmsg=" " /> <span class="Validform_checktip">*标题最多100个字符</span> </dd> </dl> <dl> <dt>发布时间</dt> <dd> <div class="input-date"> <asp:TextBox ID="txtAddTime" runat="server" CssClass="input date" onfocus="WdatePicker({dateFmt:‘yyyy-MM-dd HH:mm:ss‘})" datatype="/^\s*$|^\d{4}\-\d{1,2}\-\d{1,2}\s{1}(\d{1,2}:){2}\d{1,2}$/" errormsg="请选择正确的日期" sucmsg=" " /> <i>日期</i> </div> <span class="Validform_checktip">不选择默认当前发布时间</span> </dd> </dl> <dl> <dt>分享到上级单位</dt> <dd> <a class="clickShow hover checkBoxBtn" href="javascript:void(0);" title="">选择单位</a><span class="Validform_checktip">说明:[新闻将分享到所勾选的单位(显示在接入单位信息公开新闻栏目中]</span> <div class="artShare_CheckBox"> <label class="artShare_checkAll"> <input type="checkbox" name="checkAll" /><span class="text">全选</span></label> <ul class="artShare_List unitlist clearfix"> </ul> </div> </dd> </dl> <dl> <dt>转发到其它栏目</dt> <dd> <a class="clickShow hover checkBoxBtn" href="javascript:void(0);" title="">选择栏目</a><span class="Validform_checktip">说明:[可在其他栏目中展示该新闻,可直接放置于一级栏目下]</span> <div class="artShare_CheckBox"> <label class="artShare_checkAll"> <input type="checkbox" name="checkAll" /><span class="text">全选</span></label> <ul class="artShare_leaveOneList"> </ul> </div> </dd> </dl> </div> <div class="tab-content" style="display: none"> <dl> <dt>链接类型</dt> <dd> <div class="single-select"> <div class="boxwrap"> <a class="select-tit" href="javascript:;"><span>本文页面</span><i></i></a> <div class="select-items artEdit_urlTypeBox"> <ul> <li class="selected" data-id="0">本文页面</li> <li data-id="1">其他页面</li> <li data-id="2">文件地址</li> </ul> </div> <i class="arrow"></i> </div> </div> <span class="articleURL_input" style="display: none;"> <input type="text" id="txtCallIndex" class="input normal" /> <span class="Validform_checktip">*别名访问,非必填,不可重复</span></span><span class="articleUpload"><input type="file" class="file1" name="file1"/></span> <span class="Validform_checktip"> 说明:[1.本文页面:点击标题在详情页面查看内容;2.其他页面:点击标题打开的是链接页面;3.文件地址:点击标题直接下载文件]</span> </dd> </dl> <dl> <dt>封面图片</dt> <dd> <span><input type="file" class="file2" name="file2"/></span><span class="Validform_checktip">说明:[图片(可选)为图片新闻封面,尺寸宽度不能大于900像素,上传图片后请在内容中添加该图片,才能保证新闻内容与新闻封面图片一致]</span> </dd> </dl> <dl class="contentdata"> <dt>内容描述</dt> <dd> <%-- 在线编辑器--%> <input type="hidden" id="ContentText" runat="server" class="contentServer" /> <script id="editor" type="text/plain" name="ueContent" style="width: 99%; height: 350px;"></script> </dd> </dl> <iframe id="file_upload_return" hidden="true" name="file_upload_return"></iframe><!--用于获取表单返回的值--> </div>
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,的主要内容,如果未能解决你的问题,请参考以下文章
使用 Summernote File 一次将多个文件上传到服务器