多文件上传

C# 前台:

先[写一个JS脚本:
          <script type="text/javascript" language="Javascript">
          function addFile()
          {
              var str = '<input type="file" size="30" name="File" runat ="server"><br>';
              document.getElementById('myfile').insertAdjacentHTML("beforeend",str);            
          }
          </script>

然后再加上:
                     <p id="myfile">
                          <INPUT type="file" size="30" NAME="File" runat ="server">
                      </p>
``````````````````````````````````
                      <input type="button"    class="cbutton" value="新增附件(Add)" οnclick="addFile()" id="Button1">
                      <asp:Button ID="BT_Server"    CssClass="cbutton" runat="server" OnClick="BT_Server_Click" />

后台上传按钮就写上:                   
      protected void BT_Server_Click(object sender, EventArgs e)
      {
          string filepath = Server.MapPath("./") + "Document_file";
          string UploadFileLastName = "";
          string sUser_ProductsImageUrl = "";
          HttpFileCollection myfiles = Request.Files;
          for (int i = 0; i < myfiles.Count;i++)
          {
              HttpPostedFile userPostedFile = myfiles[i];//文件在本地的原始位置
              try
              {
                  if (userPostedFile.ContentLength > 0)
                  {
                      UploadFileLastName = userPostedFile.FileName.Substring(userPostedFile.FileName.LastIndexOf(".") + 1); //得到文件的扩展名
                      string NewUploadFileName = "";
                      //文件名称由
                      NewUploadFileName = "Document_" + "1110001111" + i.ToString() + "." + UploadFileLastName;//产生上传文件的名称
                      //imgsrc = NewUploadFileName;
                      string UserDirectory = "Document_file";//所要创建文件夹的名字
                      string UserPath = Server.MapPath("../" + UserDirectory).ToString();// + "//" + UserDirectory + "//" + "ProductsImages";
                      if (!System.IO.Directory.Exists(UserPath)) //如果文件夹不存在则创建
                      {
                          System.IO.Directory.CreateDirectory(UserPath);
                      }
                      userPostedFile.SaveAs(UserPath + "/" + NewUploadFileName);//用保存的方法将文件上载
                      sUser_ProductsImageUrl = "Document_file/" + NewUploadFileName;//得到服务端文件的虚拟路径
                  }
              }
              catch
              {
                  JScript.Alert("上传失败!");
              }
          }
      }

以上代码是本人原创,多有不足之处,请各位高手指教,谢谢!!!

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值