批量上传文件

 

 上传多文件的页面设计

绿茶配兔兔:
protected void PLUpLoad_Click(object sender, EventArgs e)
{

  //定义文件的保存路径
  string filepath = Server.MapPath("..\UploadFiles\RWGLAttachFile") + "\";
  HttpFileCollection uploadFiles = Request.Files;

  循环文件夹
  for (int i = 0; i < uploadFiles.Count; i++)
  {

    获取单个文件
    HttpPostedFile postedFile = uploadFiles[i];
    try
    {

      //如果文件是有内容的
      if (postedFile.ContentLength > 0)
     {
       Label1.Text += "已上传文件 #" + (i + 1) + ":" + System.IO.Path.GetFileName(postedFile.FileName) + "<br/>";

        postedFile.SaveAs(filepath + System.IO.Path.GetFileName(postedFile.FileName));//将文件保存到指定的路径中

        string fileType = "";

        fileType =System.IO.Path.GetExtension(postedFile.FileName)

        int slastDotIndex = postedFile.FileName.LastIndexOf(".");
        string sfileType = "";
        
if (slastDotIndex >= 0)
        {
          sfileType = postedFile.FileName.Substring(slastDotIndex).ToLower();
        }
        string postedFileNofileType = postedFile.FileName.Remove(postedFile.FileName.Length - sfileType.Length);//不带文件后缀的文件名

        第二种方式

        string fileName2 = "";
        if (ContactTel.HasFile)
        {
          fileName2 = ContactTel.ShortFileName;

          fileName2 = fileName2.Replace(":", "_").Replace(" ", "_").Replace("\", "_").Replace("/", "_");
          int lastDotIndex = fileName2.LastIndexOf(".");
          string fileType = "";
          if (lastDotIndex >= 0)
            {
            fileType = fileName2.Substring(lastDotIndex).ToLower();
            }

                                   fileName2 = DateTime.Now.Ticks.ToString() + fileType;

                                   ContactTel.SaveAs(Server.MapPath("~/UploadFiles/RWGLAttachFile/" + fileName2));

                           }

           string FlowNode, SnID;
           int TXLCAID;

          bool retVal = Govaze.SQLServerDAL.Factory.getTXLCDAL().TXLC_new_Flow_ZLTX(1, //保存为草稿为0,提交为1"SPLC", //"质量体系的审批流程",postedFile.FileName,//fileName, //相关附件
          int.Parse(TreeID.Text), //所选择的设备所在的树节点InvolvedPerson.SelectedValue,InvolvedDept.Text, //故障发生地点fileName2, //相关附件的文件路径

  postedFileNofileType,//Text101.Text,//文件名称Remark101.Text, //故障描述Remark102.Text,//相关附件的文件名称Date101.Text == "" ? null : Date101.Text,null, //故障发生日期       Flow2Person.SelectedValue, //本流程不需要,赋值为空Flow3Person.SelectedValue, //本流程不需要,赋值为空Flow4Person.SelectedValue,getUserName(), out SnID, out FlowNode, out   TXLCAID);

         if (retVal){

          PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        else{

          Alert.Show("失败,请重试!", MessageBoxIcon.Warning);
        }


         }
      }
    catch (Exception Ex)
    {
      Label1.Text += "发生错误: " + Ex.Message;
    }
    }

}













 

 

 

原文地址:https://www.cnblogs.com/sanshengshitouhua/p/14366361.html