大众计算机学习网

专门提供各种电脑教程

多文件上传示例代码

Admin | 2007-10-11 13:39:06 | 被阅次数 | 4985

<%@ Page Language="C#"%>
<%@ import namespace="System.IO"%>
<style>
body,input {font-size:12px;}
</style>
<script language="C#" runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
 
}
protected void Butupsure_Click(object sender, EventArgs e)
{
        //---在此获得上传文件的文件名----
  String savePath = Server.MapPath("upload");
  if( !Directory.Exists(savePath) )
  {
         Directory.CreateDirectory(savePath);
  }

  HttpFileCollection files  = HttpContext.Current.Request.Files;
  for(int iFile = 0; iFile < files.Count; iFile++)
  {
   HttpPostedFile postedFile = files[iFile];
      string fileName, fileExtension;
   fileName = System.IO.Path.GetFileName(postedFile.FileName);
   if (fileName != "")
   {
    fileExtension = System.IO.Path.GetExtension(fileName);
    
    postedFile.SaveAs(savePath + "\\" + fileName);

    Response.Write("<a href='upload/" + fileName + "' target=_blank>" + savePath + "\\" + fileName + "</a><br/>");
    TbMsg.Text +="<a href='upload/" + fileName + "' target=_blank>" + savePath + "\\" + fileName + "</a>\r\n";

    TbMsg.Text += "savePath=" + savePath + "\r\n";
    TbMsg.Text += "fileName=" + fileName + "\r\n";
   }
  }
  
}
</script>
<body>
<form id=form1 runat="server" ENCTYPE = "multipart/form-data">
<div id="upFiles">
</div>

<BR>上传数量:
<INPUT TYPE="text" NAME="Txtfilenum" id="Txtfilenum" value="5">
<input type="button" value="添加" onclick="Add();"/>
<script language="javascript">
function Add()
{
var up=document.getElementById ("upFiles");
var count =document.getElementById("Txtfilenum").value;
for(var i=0;i<count;i++)
{
var txt = document.createElement("input");
txt.type="file";
txt.id="fileup"+i;
txt.name = txt.id;
up.appendChild(txt);
up.appendChild(document.createElement("BR"));
}

}

</script>
<BR>
<asp:Button ID="Butupsure" runat="server" Text="上 传" OnClick="Butupsure_Click" />
<BR>
<asp:TextBox id="TbMsg" runat=server TextMode="MultiLine" width="500px" height="300px"></asp:TextBox>
</form>
</body>