asp.net多文件上传

前台:

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
    <script language="javascript" type="text/javascript">
     function addFile()
     {
        var div=document.createElement("div");
        var f=document.createElement("input");
        f.setAttribute("type","file");
        f.setAttribute("name","file");
        f.setAttribute("size","50");
        div.appendChild(f);
        var b=document.createElement("input");
        b.setAttribute("type","button");
        b.setAttribute("value","移除");
        b.setAttribute("onclick","remove(this)");
        div.appendChild(b);
        document.getElementById("divFiles").appendChild(div);
     }
     function remove(o)
     {
        while(o.tagName!="div")
        o=o.parentNode;
        o.parentNode.removeChild(0);
     }
    </script>
</head>
<body>
    <form id="form1" runat="server" enctype="multipart/form-data">
    <div runat="server" id="divFiles">
    <input type="file"  size="50" name="file" />
    </div>
    <div>
    <input type="button" value="添加文件" onclick="addFile()" />
    </div>
    <asp:Button ID="btnUpload" Text="上传"  runat="server" onclick="btnUpload_Click"/>
    </form>
</body>
</html>
后台:

protected void btnUpload_Click(object sender, EventArgs e)
        {
            HttpFileCollection files = HttpContext.Current.Request.Files;
            for (int i = 0; i < files.Count; i++)
            {
                HttpPostedFile file = files[i];
                string fileName = System.IO.Path.GetFileName(file.FileName);
                string fileExtension = System.IO.Path.GetExtension(file.FileName);
                file.SaveAs(System.Web.HttpContext.Current.Request.MapPath("images/") + fileName);
            }
        }

你可能感兴趣的:(asp.net多文件上传)