最好的多附件上傳,無私奉獻了!

    <script language="javascript" type="text/javascript">
        function addFileControl()
        {
            var index = parseInt(document.getElementById("Hidden1").value,typeof(int));
            addFile(index);
        }
        function addFile(index)
        {
            var str = "<input type = 'file' id = 'file" + index + "' name = 'File' style='width: 350px'/><input type='button' id='button" + index +"' οnclick='delFileControl(" + index + ");' value='刪除' class='cmd_defaultsmall'/>";
            document.getElementById("fileList").insertAdjacentHTML("beforeEnd",str);
            document.getElementById("Hidden1").value = eval(index + 1);
        }
        function delFileControl(index)
        {
            document.getElementById("fileList").removeChild(document.getElementById("file" + index));
            document.getElementById("fileList").removeChild(document.getElementById("button" + index));
        }
    </script>                                           

 <table border="0" cellpadding="0" cellspacing="0" width="744" class="table_main_2"                   style="background-color: white">
                                                <tr>
                                                    <td align="right" colspan="2" rowspan="1" style="height: 5px">
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td align="right" rowspan="2">
                                                        附件:</td>
                                                    <td rowspan="1">
                                                        <table cellpadding="0" cellspacing="0" width="100%" id="tblAttachFile" runat="server">
                                                            <tr>
                                                                <td>
                                                                    <div id="fileList0" runat="server" style="width: 531px">
                                                                    </div>
                                                                </td>
                                                            </tr>
                                                            <tr>
                                                                <td>
                                                                    <input id="Button1" class="cmd_defaultsmall" οnclick="addFileControl('fileList0')"
                                                                        type="button" value="增加附件" /><asp:Button ID="btnSaveFile" runat="server" CssClass="cmd_defaultsmall"
                                                                            OnClick="btnSaveFile_Click" Text="上傳" /></td>
                                                            </tr>
                                                        </table>
 

 

 

    /// <summary>
    /// 上傳選定的文件的具體實現
    /// </summary>
    /// <returns></returns>
    private bool UpMoreFile()
    {
        System.Web.HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
        OA.Layout.Entities.OALayoutLicence layoutLicence = ReturnLayoutLicence();

        try
        {
            string fileName, physicalPath;

            physicalPath = Server.MapPath(UploadRoot + SUBPATHROOT + @"/" + layoutLicence.ApplyID.ToString());
            System.IO.Directory.CreateDirectory(physicalPath);
            for (int i = 0; i < files.Count; i++)
            {
                System.Web.HttpPostedFile postedFile = files[i];
                fileName = System.IO.Path.GetFileName(postedFile.FileName).Replace(" ", "");
                if (fileName != string.Empty)
                {
                    OA.Layout.Entities.OALayoutLicenceC layoutLicenceCFiles = new OA.Layout.Entities.OALayoutLicenceC();
                    layoutLicenceCFiles.FileName = fileName;
                    layoutLicenceCFiles.Path = SUBPATHROOT + @"/" + layoutLicence.ApplyID.ToString() + "//" + fileName;


                    if (layoutLicence.OALayoutLicenceCCollection.Find("FileName", layoutLicenceCFiles.FileName) != null)
                    {
                        WriteJSToClient("<script language=javascript>alert('附件名稱重複!請重新上傳附件!');</script>");
                        continue;
                        //return false;
                    }
                    layoutLicence.OALayoutLicenceCCollection.Add(layoutLicenceCFiles);
                    postedFile.SaveAs(physicalPath + @"/" + fileName);
                }
            }
            Save();
        }
        catch (Exception ex)
        {
            ShowMessage("上傳失敗:" + Server.HtmlEncode(ex.Message));
            return false;
        }
        return true;

    }

發佈了45 篇原創文章 · 獲贊 2 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章