html控件自动点 “加号”添加 多个附件

HTML:

<form id="form1" runat="server" enctype="multipart/form-data">

<input id="FileTI1" name="FileTI1" type="file" style="width: 300px;" /><br />
                <div id="divTI">
                </div>
<input id="btnAddAttTI1" type="button" value="添加附件" οnclick="btnAddAttTI();" />

 

 

JS:

var ti = 1;

function btnAddAttTI() {
            ti += 1;
            $("#divTI").append("<div id=\"divTI" + ti + "\"><input id=\"FileTI" + ti + "\" name=\"FileTI" + ti + "\" type=\"file\" style=\"width: 300px;\" /><img src='images/del.gif'  style='cursor: pointer;' width='15' height='15' οnclick=\"deleteTI('" + ti + "')\" alt='删除附件' /> <br /></div>")
        }
        function deleteTI(tiid) {
            $("#divTI" + tiid + "").remove();
        }

 

 

c#

/// <summary>
        /// 上传附件
        /// </summary>
        /// <returns></returns>
        private bool UploadAtt(string pId, string htNo)
        {
            bool R = false;
            HttpFileCollection files = HttpContext.Current.Request.Files;
            string pathML = "", pathTI = "", pathCK = "", pathZB = "", pathXHT = "", pathYS = "";
            string dML = "", dTI = "", dCK = "", dZB = "", dXHT = "", dYS = "";
            #region 创建保存附件目录
            if (files.Count != 0)
            {
                string path = Server.MapPath("UploadFile");
                 pathML = path + "/" + pid + "/" + htno + "/ml";
                dML = "BusinessUploadFile/" + pid + "/" + htno + "/ml";
                //创建目录
                if (!Directory.Exists(path + "/" + pid + "/" + htno + "/ml"))
                {
                    Directory.CreateDirectory(path + "/" + pid + "/" + htno + "/ml");
                }
            




            }

            #endregion
            int BZ = 0;
            for (int iFile = 0; iFile < files.Count; iFile++)
            {
                HttpPostedFile postedFile = files[iFile];
                if (!string.IsNullOrEmpty(postedFile.FileName))
                {
                    BZ = 1;
                    string fileName = "";//定义文件名
                    fileName = Path.GetFileName(postedFile.FileName);//得到上传文件的完整名称 即文件名+后缀名

                    //int index = fileName.IndexOf(".");
                    string tempPath = "", dPath = "", ctype = ""; ;
                    if (files.AllKeys[iFile].IndexOf("FileML") > -1)
                    {
                        tempPath = pathML;
                        dPath = dML;
                        ctype = "ML";
                    }



                    string mypath = tempPath + "/" + fileName;

                    try
                    {
                        postedFile.SaveAs(mypath);   //保存附件
                        //添加附件信息数据
                        BusinessAttModel uam = new BusinessAttModel();
                        uam.AttName = fileName.Substring(0, fileName.IndexOf('.'));
                        uam.ProjectId = int.Parse(pId);
                        uam.AttPath = dPath + "/" + fileName;
                        uam.EmployeeID = Session["LoginUser"].ToString();
                        uam.CHTNo = htNo;
                        uam.CType = ctype;
                        PB.BusinessSaveAtt(uam);
                        R = true;
                    }
                    catch (Exception ex)
                    {
                        PB.DeleteBusinessAtt(pId, htNo, ctype);  //捕获到异常就删除当前附件的记录 遗留附件没关系,在上传可以覆盖
                        R = false;
                        break;
                    }
                }
            }
            if (BZ == 0)   //没上传附件
            {
                R = true;
            }
            return R;
        }

转载于:https://www.cnblogs.com/PeaCode/p/3881408.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值