多文档上传功能实现

.aspx
<script type="text/javascript">
 function addfile()
 {
 var str='<INPUT id="upfile1" type="file" size="24" name="upfile1" style="BORDER-RIGHT: dimgray 1px solid; BORDER-TOP: dimgray 1px solid; BORDER-LEFT: dimgray 1px solid; WIDTH: 264px; BORDER-BOTTOM: dimgray 1px solid;"><br>'
 document.getElementById('myfile1').insertAdjacentHTML("beforeEnd",str)   
 }  
</script>
 
<table width="500" align="center">
     <tr>
           <td valign="top" width="120">
                 <asp:Label ID="lblselect" runat="server" Text="请选择文件路径"></asp:Label>
           </td>
           <td width="266">
                 <p id="myfile1">
                 <input id="upfile1" style="BORDER-RIGHT: dimgray 1px solid; BORDER-TOP: dimgray 1px solid; BORDER-LEFT: dimgray 1px solid; WIDTH: 264px; BORDER-BOTTOM: dimgray 1px solid; "  type="file" size="24" name="upfile4" runat="server">
                 </P> 
            </td>
            <td valign="top">
                 <input style="BORDER-RIGHT: 1px ridge; BORDER-TOP: 1px ridge; BORDER-LEFT: 1px ridge; WIDTH: 75px; BORDER-BOTTOM: 1px ridge; HEIGHT: 20px"  onclick="addfile()" type="button" size="14" value="New Control">                            
            </td>
     </tr>
     <tr>
            <td></td>
            <td>
                 <asp:Button ID="btnload" runat="server" Height="22px" Text="导入系统"  Width="64px" onclick="btnload_Click" />
                 <asp:Button ID="Btncancel" runat="server" Height="22px" Text="取  消" />
                 <asp:Button ID="Btnclose" runat="server" Height="22px" Text="关  闭" onclick="Btnclose_Click" />
            </td>
            <td></td>
     </tr>
</table>
 
.cs
protected void Load_File()
{
        HttpFileCollection files = HttpContext.Current.Request.Files;
        try
        {
            for (int iFile = 0; iFile < files.Count; iFile++)
            {
                HttpPostedFile postedFile = files[iFile];
                string filename, strServerFileName;//, fileExtension;
                filename = System.IO.Path.GetFileName(postedFile.FileName);
                if (filename != "")
                {
                    //fileExtension = System.IO.Path.GetExtension(filename);获取filename文件扩展名                   
                    strServerFileName =System.IO.Path.GetDirectoryName(Server.MapPath("login.aspx")) + "\\Bom\\" + filename;
                    //将WebForm1.aspx所在的文件目录\上传的文件名赋给strServerFileName变量
                    postedFile.SaveAs(strServerFileName);//保存上载文件的内容                   
                    ReadExcel(strServerFileName, GridView1);
                }
            }
            this.RefreshOpener();
        }
        catch (Exception ex)
        {
            lblmsg.Text = ex.Message;
            return;
        }
    }
原文地址:https://www.cnblogs.com/lovewife/p/1396475.html