基于file上传文件的并发上传(多个文件一起上传到后台并把数据存储的同一条数据中,如 数据库字段videopath,imge。前台发送来的文件file1,file2。 videopath=file1,imge=file2)

前台代码:

    
class="tab-content">
所属栏目
class="rule-single-select"> <select id="ddlCategoryId"> select>
推荐类型
class="rule-multi-checkbox"> "checkboxs"> "checkbox" class="checked" id="PL" />"checkbox" class="checked" id="ZD" />"checkbox" class="checked" id="TJ" />"checkbox" class="checked" id="JY" />
文章标题
"txtTitle" runat="server" CssClass="input normal" datatype="*2-100" sucmsg=" " /> class="Validform_checktip">*标题最多100个字符
发布时间
class="input-date"> "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=" " /> 日期
class="Validform_checktip">不选择默认当前发布时间
分享到上级单位
class="clickShow hover checkBoxBtn" href="javascript:void(0);" title="">选择单位<span class="Validform_checktip">说明:[新闻将分享到所勾选的单位(显示在接入单位信息公开新闻栏目中]
class="artShare_CheckBox">
    class="artShare_List unitlist clearfix">
转发到其它栏目
class="clickShow hover checkBoxBtn" href="javascript:void(0);" title="">选择栏目<span class="Validform_checktip">说明:[可在其他栏目中展示该新闻,可直接放置于一级栏目下]
class="artShare_CheckBox">
    class="artShare_leaveOneList">
class="tab-content" style="display: none">
链接类型
class="single-select">
class="boxwrap"> class="select-tit" href="javascript:;">本文页面
class="select-items artEdit_urlTypeBox">
  • class="selected" data-id="0">本文页面
  • "1">其他页面
  • "2">文件地址
class="arrow">
class="articleURL_input" style="display: none;"> "text" id="txtCallIndex" class="input normal" /> class="Validform_checktip">*别名访问,非必填,不可重复class="articleUpload">"file" class="file1" name="file1"/> class="Validform_checktip"> 说明:[1.本文页面:点击标题在详情页面查看内容;2.其他页面:点击标题打开的是链接页面;3.文件地址:点击标题直接下载文件]
封面图片
"file" class="file2" name="file2"/>class="Validform_checktip">说明:[图片(可选)为图片新闻封面,尺寸宽度不能大于900像素,上传图片后请在内容中添加该图片,才能保证新闻内容与新闻封面图片一致]
class="contentdata">
内容描述
<%-- 在线编辑器--%> "hidden" id="ContentText" runat="server" class="contentServer" />

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($('', { name: 'MenuId', value: tmp.xj_MenuId }));
    pams.push($('', { name: 'Msg', value: tmp.is_Msg }));
    pams.push($('', { name: 'Title', value: tmp.xj_Title }));
    pams.push($('', { name: 'AddTime', value: tmp.xj_AddTime }));
    if (tmp.LinkType == 1) {
        pams.push($('', { name: 'TitleLink', value: tmp.TitleLink }));
    }
    if (tmp.LinkType == 0) {
        pams.push($('', { name: 'Content', value: tmp.xj_Content }));
    }
    if (tmp.LinkType == 2) {
        var file1 = $(".file1").val();
        if (file1 == "") {
            jsprint("文件不能为空", '', 'Error');
            return false;
        }
    }
    pams.push($('', { name: 'LinkType', value: tmp.LinkType }));
    pams.push($('', { name: 'EduEnable', value: tmp.xj_EduEnable }));
    pams.push($('', { name: 'RelayMenu', value: tmp.xj_RelayMenu }));
    pams.push($('', { name: 'Top', value: tmp.is_Top }));
    pams.push($('', { name: 'Red', value: tmp.is_Red }));
    pams.push($('', { name: 'IsLocalNet', value: tmp.xj_IsLocalNet }));
    pams.push($('', { name: 'op', value: "add_edit_article" }));
    pams.push($('', { name: 'state', value: "Add" }));
    pams.push($('', { name: 'navName', value: navName }));
    var imgfile = $(".file2").val();
    if (imgfile == "") {
        jsprint("封面图不能为空", '', 'Error');
        return false;
    }
    var turnForm = document.createElement("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))