html控制項自動點 “加號”添加 多個附件

來源:互聯網
上載者:User

標籤:style   http   os   strong   io   檔案   資料   for   

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="添加附件" onclick="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‘ onclick=\"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;
        }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.