ASP.NET实现多文件上传

本文介绍了一个使用ASP.NET实现的多文件上传功能,包括前端界面设计和后端处理逻辑。前端通过JavaScript动态添加文件输入框,后端则用C#接收并处理上传的文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<script language="javascript">
  function addFileControl()
  {
  var str='<input type=file name=File>'
  document.getElementById('FileCollection').insertAdjacentHTML("beforeEnd",str)
  }
            </script>
      </HEAD>
      <body MS_POSITIONING="GridLayout">
            <form id="uoMoreFile" method="post" runat="server" enctype="multipart/form-data">
                  <asp:Label Runat="server" ID="Title"></asp:Label>
                  <p id="FileCollection"><input type="file" size="50" name="File"></p>
                  <p>
                        <input type="button" value="增加(File)" onclick="addFileControl()">
                        <asp:Button Runat="server" Text="上传" id="Upload"></asp:Button>
                        <input onclick="this.Form.reset()" type="button" value="重置"></p>
                  <p align="center"><asp:Label ID="strStatus" Runat="server" Width="500px"></asp:Label></p>
            </form>
      </body>
后台CS
protected System.Web.UI.WebControls.Label Title;
  protected System.Web.UI.WebControls.Button Upload;
  protected System.Web.UI.WebControls.Label strStatus;
  private void Page_Load(object sender, System.EventArgs e)
  {
   Title.Text="<h3>多文件上传</h3>";
   // 在此处放置用户代码以初始化页面
  }
  private void Upload_Click(object sender, System.EventArgs e)
  {
   upMorefile();
  }
  private void upMorefile()
  {
   System.Web.HttpFileCollection files=System.Web.HttpContext.Current.Request.Files;
   System.Text.StringBuilder strMsg=new System.Text.StringBuilder("上传的文件信息分别为:<hr color=red>");
   int fileCount;
   int filecount=files.Count;
   try
   {
    for(fileCount=0;fileCount<files.Count;fileCount++)
    {
     //定义访问客户端上传文件的对象
     System.Web.HttpPostedFile postedFile=files[fileCount];
     string fileName,fileExtension;
     fileName=System.IO.Path.GetFileName(postedFile.FileName);
     if(fileName!=string.Empty)
     {
      //取的上传文件的扩展名
      fileExtension=System.IO.Path.GetExtension(fileName);
      //上传的文件信息
      strMsg.Append("上传文件的类型:"+postedFile.ContentType.ToString()+"<br>");
      strMsg.Append("客户端文件地址:"+postedFile.FileName+"<br>");
      strMsg.Append("上传文件的文件名:"+fileName+"<br>");
      strMsg.Append("上传文件的扩展名"+fileExtension+"<br><hr color=red>");
      //保存到指定的文件夹
      postedFile.SaveAs(Server.MapPath("upedFile\\")+fileName);
     }

    }
    strStatus.Text=strMsg.ToString();
    return true;
   }
   catch(System.Exception error)
   {
    strStatus.Text=error.ToString();
    return false;
   }
  }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值