两种多文件上传

来源:互联网 发布:摩根华鑫证券知乎 编辑:程序博客网 时间:2024/06/09 22:48

第一种:

<head runat="server">
    <title></title>
    <script type="text/javascript">
    <!--
        var MAXFILES = 5; 
        //文件计数器 
        var fileCount = 0; 
        function addAttach(noAlert) { 
            if (fileCount >= MAXFILES && !noAlert) { 
                alert("最多只能添加" + MAXFILES + "个附件!"); 
                return; 
            } 
            //找到files这个div
            var fileSectionDiv = document.getElementById("files"); 

            //定义创建一个div
            var fileItemDiv = document.createElement("div"); 
            //计数器加1
            fileCount++; 
 
             //定义一个上传的组件
            var content = "<input type='file' onchange='return addAttach(true);' name='fileUpload'" + fileCount + ">&nbsp;<a href='#' onclick='return delAttach(/"" + fileCount + "/")' class='delete_attach' >移除附件</a>";

            //dilwItemDiv的id为fileItemDiv1、fileItemDiv2、
            fileItemDiv.id = "fileItemDiv" + fileCount; 
            //创建一个content上传组件
            fileItemDiv.innerHTML = content; 
            //创建一个div
            fileSectionDiv.appendChild(fileItemDiv); 
 
            return false; 
        } 
         //删除附件
        function delAttach(fileIndex) { 
            var fileSectionDiv = document.getElementById("files");
            var fileItemDiv = document.getElementById("fileItemDiv" + fileIndex); 
            //移除附件
            fileSectionDiv.removeChild(fileItemDiv); 
            return false; 
        } 
    -->
    </script>
    <style type="text/css">
    .delete_attach { 
    PADDING-LEFT: 18px; BACKGROUND: url(images/deleteattch_icon.gif) no-repeat left top; MARGIN-LEFT: 7px; WIDTH: 90px; COLOR: #002f76 

 
.add_attach { 
    PADDING-LEFT: 22px; BACKGROUND: url(images/attach.gif) no-repeat left center; WIDTH: 90px; COLOR: #002f76 
}
    </style>
</head>
<body>
    <form id="form1" runat="server" enctype="multipart/form-data">
    <div>
    多文件上传<br /><br/><br/><br/><br/>
            <a id="addAttach_a" onclick="return addAttach(false);" href='#'  class="add_attach">添加附件</a>  
        <div id="files"></div> 
        <asp:Button ID="btnSend" runat="server" Text="发送" onclick="btnSend_Click" />
       

    </div>

    </form>

 

 

    protected void btnSend_Click(object sender, EventArgs e)
    {
        for (int index = 0; index < Request.Files.Count; index++)
        {
            if (!string.IsNullOrEmpty(Request.Files[index].FileName))
            {
                Request.Files[index].SaveAs(Path.Combine(Server.MapPath("~/UploadFiles/"), System.IO.Path.GetFileName(Request.Files[index].FileName)));
                Response.Write("<script>alert(/"上传成功!/")</script>");
            }
        }
    }

 

 

 

 

 

 

第二种:

<head runat="server">
    <title></title>
      <script language="JavaScript" type="text/javascript">
          function addFileControl() {
              var str = '<br><INPUT type="file" NAME="File">'
              document.getElementById('FileCollection').insertAdjacentHTML("beforeEnd", str)
          }
  </script>
</head>
<body>
    <form id="form1" runat="server" enctype="multipart/form-data">
        <div>
           <asp:label id="Title" Runat="server"></asp:label>
   <p id="FileCollection"><input type="file" name="File"/>
   </p>
   <p><input onclick="addFileControl()" type="button" value="增加(File)" runat="server"/>
    <asp:button id="Upload" Runat="server" Text="上传" Width="56px" runat="server"
                    onclick="Upload_Click"></asp:button>
                <input style="WIDTH: 56px; HEIGHT: 24px" onclick="this.form.reset()" type="button" runat="server" value="重置"/>
   </p>
   <p align="center"><asp:label id="strStatus" runat="server" BorderColor="White" BorderStyle="None" Width="500px"
     Font-Size="9pt" Font-Bold="True" Font-Names="宋体"></asp:label></p>
        </div>
    </form>
</body>

 

 

 

    protected void Upload_Click(object sender, EventArgs e)
    {
        upMorefile();
    }
    private bool upMorefile()
    {
        //遍历File表单元素
        System.Web.HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
        //状态信息
        System.Text.StringBuilder strMsg = new System.Text.StringBuilder("上传的文件信息分别为:<hr color=red>");
        int fileCount;
        int filecount = files.Count;
        try
        {
            for (fileCount = 0; fileCount < files.Count; fileCount++)
            {
                //定义访问客户端上传文件的对象
                System.Web.HttpPostedFile postedFile = files[fileCount];
                string fileName, fileExtension;
                //取得上传得文件名
                fileName = System.IO.Path.GetFileName(postedFile.FileName);
                if (fileName != String.Empty)
                {
                    //取得文件的扩展名
                    fileExtension = System.IO.Path.GetExtension(fileName);
                    //上传的文件信息
                    strMsg.Append("上传的文件类型:" + postedFile.ContentType.ToString() + "<br>");
                    strMsg.Append("客户端文件地址:" + postedFile.FileName + "<br>");
                    strMsg.Append("上传文件的文件名:" + fileName + "<br>");
                    strMsg.Append("上传文件的扩展名:" + fileExtension + "<br><hr color=red>");
                    //保存到指定的文件夹
                    postedFile.SaveAs(Server.MapPath("~/UploadFiles/") + fileName);
                }
            }
            strStatus.Text = strMsg.ToString();
            return true;
        }
        catch (System.Exception error)
        {
            strStatus.Text = error.Message;
            return false;

        }
    }

原创粉丝点击