.net 多文件上传

来源:互联网 发布:php ajax json 实例 编辑:程序博客网 时间:2024/05/17 15:41

 前台:
C# code<mce:style><!--
#tab {
border:1px solid #a4d140;
border-collapse:collapse;
width:400px;
}
#tab th{
border:1px solid #aaaaaa;
background:#cce98b;
border-collapse:collapse;
text-align:left;
}
#tab td{
border:1px solid #cce98b;
border-collapse:collapse;
}
.txt{
   border:1px solid #888;
background:#ECE9D8;
}
.checkBg{
border:1px solid #a4d140;
background:#ECE9D8;
}
.button {
   border:1px solid #a4d140;margin:5px 0px;
   width:80px;background:#cce98b;height:22px;line-height:22px;
}
--></mce:style><style mce_bogus="1">#tab {
border:1px solid #a4d140;
border-collapse:collapse;
width:400px;
}
#tab th{
border:1px solid #aaaaaa;
background:#cce98b;
border-collapse:collapse;
text-align:left;
}
#tab td{
border:1px solid #cce98b;
border-collapse:collapse;
}
.txt{
   border:1px solid #888;
background:#ECE9D8;
}
.checkBg{
border:1px solid #a4d140;
background:#ECE9D8;
}
.button {
   border:1px solid #a4d140;margin:5px 0px;
   width:80px;background:#cce98b;height:22px;line-height:22px;
}</style>
<script language="javascript" type="text/javascript"><!--
function $(id) {return document.getElementById(id);}
function $F(name){return document.getElementsByTagName(name);}

function add(){
var otr = document.getElementById("tab").insertRow(-1);
var checkTd=document.createElement("td");
checkTd.innerHTML = '<input name="delx"  type="button" class="button" onclick="del();" value="删除" />';
var otd1 = document.createElement("td");
otd1.innerHTML = '<input type="file" id="File1" name="File1" size="30"  runat="server"  />';

otr.appendChild(checkTd);
otr.appendChild(otd1);
}
function del()
{
    var dgTable=document.getElementById("tab").rows;
    var pp=event.srcElement;
       
    for (var i=0; i < dgTable.length; i++)
    {
         if(pp==dgTable[i].cells[0].getElementsByTagName("input")[0])
         {
            document.getElementById("tab").deleteRow(i);
         }
        
    }
}
-->

</script>
<input name="addv_btn" id="addv_btn" type="button" class="button" onclick="add();" value="添加" />

<table id="tab" name="tab" >
<tr>
<th width="100px"></th>
<th width="300px">文件</th>
</tr>
</table>
<asp:Button ID="Button1" runat="server" CssClass="button" Text="确认上传" OnClick="Button1_Click" /><br />
<asp:Label ID="errorLabel" runat="server"></asp:Label>

后台:
C# code
    static public string _SavePath;
    static public string[] FileNames;
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    public string[] AllFiles
    {
        get {
            return FileNames;
        }
    }
    public string SavePath
    {
        set
        {
             _SavePath=value;
        }
    }
    void GetControlsToUpload()    
    {
        HttpFileCollection files = HttpContext.Current.Request.Files;
        FileNames=new string[files.Count];
        for (int i = 0; i < files.Count; i++)
        {
            HttpPostedFile file = files[i];
            UpFile(file,i);
               
        } 
    }
    protected void UpFile(HttpPostedFile File,int Index)
    {
        if (File.FileName != ""&&File.FileName !=null)
        {
            string oldName = File.FileName.Substring(File.FileName.LastIndexOf('//') + 1, File.FileName.Length - File.FileName.LastIndexOf('//')-1);
            string newName = DateTime.Now.ToString("yyyyMMddhhmmss") + "_" +oldName ;
            try
            {
                File.SaveAs(Server.MapPath(_SavePath + newName));
                errorLabel.Text += File.FileName+"上传成功<br>";
                FileNames[Index]=newName;

            }
            catch
            {
                errorLabel.Text += File.FileName+"上传失败<br/>";
            }

        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        GetControlsToUpload();
    }

原创粉丝点击