Dzwebs.Net

撰写电脑技术杂文十余年

多文件上传示例代码

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

温馨提示!

如果未能解决您的问题,请点击搜索;登陆可复制文章,点击登陆

<%@ 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>


该杂文来自: 网站开发杂文

上一篇:用SqlCommandBuilder 实现批量更新

下一篇:asp.net(c#)发送邮件代码

网站备案号:

网站备案号:滇ICP备11001339号-7

版权属性:

Copyright 2007-2021-forever Inc. all Rights Reserved.

联系方式:

Email:dzwebs@126.com QQ:83539231 访问统计