上传附件(满1000个文件则自建新文件夹)

 /// <summary>
        /// 上传附件按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void send_btn_Click(object sender, EventArgs e)
        {
            this.report_submit_state.Text = "";
            string UserDirectory = "Attachment";//所要创建文件夹的名字
            try
            {
                string addition_Index_New = null;
                string addition_Index = (int.Parse(ViewState["addition_Index"].ToString()) + 1).ToString();
                if (int.Parse(addition_Index) > 5)
                {
                    report_submit_state.Text = "附件数不能超过5个!";
                    return;
                }
                else
                {
                    if (SaveReportByState("1"))
                    {
                        report_submit_state.Text = "保存成功!";

                        int dNum = 1;
                        report_submit_state.Text = "";
                        if (addition_FileUpload.HasFile)//如果选择了要导入的文件
                        {
                            if (addition_FileUpload.PostedFile.ContentLength <= 2097152)
                            {

                                string memberID = ViewState["member_badge"].ToString();
                                string yearNum = this.labor_year.Text;
                                string weekNum = this.labor_week.Text;
                                WeeklyReport wr = new WeeklyReport();
                                wr = wrh.LoadReportAllByRID(memberID, yearNum, weekNum);
                                int repID;
                                repID = wr.ID;
                                string firstNameIndex = memberID + "_" + yearNum + "_" + weekNum + "_";
                                List<WeeklyReportAttachment> list_wra_index = new List<WeeklyReportAttachment>();
                                List<int> wraIndex = new List<int>();
                               // wraIndex =null;
                                list_wra_index=wrh.SeleWraByIdYearWeek(firstNameIndex);
                                if (list_wra_index != null)
                                {
                                    foreach (WeeklyReportAttachment wrrr in list_wra_index)
                                    {
                                        string wraIn = wrrr.FileName.Substring(wrrr.FileName.LastIndexOf("_") + 1).ToString();
                                        wraIndex.Add(int.Parse(wraIn));
                                    }
                                }
                                if (wraIndex != null)
                                {

                                    addition_Index_New = GetSmallNum(wraIndex).ToString();
                                }
                                else
                                {
                                    addition_Index_New = "1";
                                }
  string filename = memberID + "_" + yearNum + "_" + weekNum + "_" + addition_Index_New;
                               

                                int current_dNum = creatDirectory(dNum, filename);// 上传文件
                                if (current_dNum != -1)
                                {
                                    string houzhui = (Path.GetExtension(addition_FileUpload.FileName).ToLower().Substring(1));
                                    string fileContent = Server.MapPath(Request.ApplicationPath) + @"\files\" + UserDirectory + "\\" + "AttachmentPart_" + current_dNum + "\\" + filename + "." + houzhui;
                                    WeeklyReportAttachment wra = new WeeklyReportAttachment();
                                    wra.MemberID = ViewState["member_badge"].ToString();
                                    wra.ReportID = repID;
                                    wra.FileName = filename;
                                    wra.FileLink = fileContent;
                                    if (wrh.AddReportAc(wra))
                                    {
                                        report_submit_state.Text = "附件添加成功!";
                                        int currentAI = int.Parse(ViewState["addition_Index"].ToString()) + 1;
                                        ViewState["addition_Index"] = currentAI.ToString();
                                        SeleWraByIdYearWeek(memberID, yearNum, weekNum);//查询
                                    }
                                    else
                                    {
                                        report_submit_state.Text = "附件添加失败!";
                                        return;
                                    }
                                }
                                else
                                {
                                    return;
                                }

                            }
                            else
                            {
                                report_submit_state.Text = "附件过大!";
                            }
                        }
                        else
                        {
                            report_submit_state.Text = "没有选择附件!";
                        }
                    }
                    else
                    {
                        report_submit_state.Text = "操作失败!";
                    }
                }
            }
            catch (Exception ex)
            {
                Response.Redirect("/ExceptionPage.aspx");
            }
        }


        /// <summary>
        ///创建文件夹,若文件加内文件数〉1000则新建文件夹,后缀为dnum+1.
        /// </summary>
        /// <param name="dNum"></param>
        public int creatDirectory(int dNum,string filename)
        {
            string houzhui = (Path.GetExtension(addition_FileUpload.FileName).ToLower().Substring(1));
             string UserDirectory = "Attachment";//所要创建文件夹的名字
             string pateu = Server.MapPath(Request.ApplicationPath) + @"\files\" + UserDirectory + "\\" + "AttachmentPart_" + dNum+"\\";
             string UserPath = Server.MapPath(Request.ApplicationPath) + @"\files\" + UserDirectory + "\\" + "AttachmentPart_"+dNum;
             if (!Directory.Exists(UserPath)) //如果文件夹不存在则创建
             {
                 Directory.CreateDirectory(UserPath);
                 try
                 {
                     string FilePath = Server.MapPath(Request.ApplicationPath) + @"\files\" + UserDirectory + @"\" + "AttachmentPart_" + dNum + @"\" + filename + "." + houzhui;
                     addition_FileUpload.SaveAs(FilePath);  //上传文件
                     return dNum;
                 }
                 catch (Exception ex)
                 {
                     report_submit_state.Text = "文件上传有误!";
                     return -1;
                 }
                
             }
             string[] filesLen = Directory.GetFiles(pateu, "*");
             if (int.Parse(filesLen.Length.ToString()) >= 2)
             {
                 dNum = dNum + 1;
                 creatDirectory(dNum,filename);   //dNum+! 调用自己
                 return dNum;  //ruturn到最外层
             }
             try
             {
                 string FilePath = Server.MapPath(Request.ApplicationPath) + @"\files\" + UserDirectory + @"\" + "AttachmentPart_" + dNum + @"\" + filename + "." + houzhui;
                 addition_FileUpload.SaveAs(FilePath);  //上传Excel文件
                 return dNum;
             }
             catch (Exception ex)
             {
                 report_submit_state.Text = "文件上传有误!";
                 return -1;
             }
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值