多附件上传的方法

来源:互联网 发布:淘宝减肥药排行榜 编辑:程序博客网 时间:2024/06/04 18:27

前台

    <script language="JavaScript">
    function addFileControl()
    {
         var str = '<br><INPUT type="file"   style=" HEIGHT: 22px" > ';
         document.getElementById('FileCollection').insertAdjacentHTML("beforeEnd",str);
    }
  </script>

  <form method="post" runat="server" enctype="multipart/form-data">

页面中加入下列控件还有一个web控件button

<div >
           <input type="file" style="width: 428px; height: 22px" />
</div>
<input type="button" value="增加图片" />

后台代码

 

 

  //上传文件函数

 

  private void  upMorefile()
  {
      System.Web.HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;//遍历File表单元素
      System.Text.StringBuilder strMsg = new System.Text.StringBuilder("上传的文件信息分别为:<hr color=red>");//状态信息


   try
   {
    for(int 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(paths) + fileName);//保存到指定的文件夹
         }
      }
   }
   catch(System.Exception error)
   {strStatus.Text = error.Message;


  }

 

 

Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=741114