2014-10-13 18 views
1

첫 번째 코드에서는 데이터 테이블에 두 개의 데이터베이스 테이블과 데이터를 가져 와서 스트림 라이터의 도움으로 CSV 파일의 모든 데이터를 쓰고 폴더에있는 파일과 압축 형식으로 다운로드 할 때가 있지만 오류가 발생하는 경우가 있습니다. 다른 프로세스에서 사용 중이기 때문에 프로세스가 'xxxxx.zip'파일에 액세스 할 수 없습니다.ctv에 Datatable은 다른 프로세스에서 사용하고 있기 때문에 다른 프로세스에서 사용하고 있습니다.

protected void btnExportToCSV_Click(object sender, EventArgs e) 
    { 
    //Work Detail 
    blExportToExcel obj = new blExportToExcel(); 
    System.Data.DataTable dtTechSanc = blDbFunction.GetTechSanc(ddlAgency.SelectedValue.ToString(), ddlDivision.SelectedValue.ToString(), ddlDistrict.SelectedValue.ToString(), ddlMC.SelectedValue.ToString(), ddlScheme.SelectedValue.ToString(), ddlUserType.SelectedValue.ToString(), ddlWorkType.SelectedValue.ToString(), ddlWorkNature.SelectedValue.ToString(), ddlWorkStatus.SelectedValue.ToString(), ((prpSessionData)(Session["sUserInfo"])).UId); 
    dtTechSanc.Columns["Id"].ColumnName = "WorkCode"; 
    dtTechSanc.Columns["TSId"].ColumnName = "Id"; 
    string filenamezip = "ZipFile\\TechSancRevision_" + DateTime.Now.ToString().Replace(":", "-").Replace("/", "-") + ".zip"; 
    string strPathAndQuery = HttpContext.Current.Request.PhysicalApplicationPath; 
    string paths = strPathAndQuery + filenamezip; 
    ZipFile z = ZipFile.Create(paths); 
    z.BeginUpdate(); 

    if (dtTechSanc.Rows.Count > 0) 
    { 

     string tmpPath = Server.MapPath("FileTemplates"); 
     string tmpFileName = "TechSancRevision.csv"; 
     tmpPath = @"" + tmpPath.Replace("/", "\\").Replace("\\Department", "") + "\\" + tmpFileName; 

     StreamWriter sw = new StreamWriter(tmpPath, false); 
     int columnCount = dtTechSanc.Columns.Count; 
     for (int i = 0; i < columnCount; i++) 
     { 
      sw.Write(dtTechSanc.Columns[i]); 
      if (i < columnCount - 1) 
      { 
       sw.Write(","); 
      } 
     } 
     sw.Write(sw.NewLine); 
     foreach (DataRow dr in dtTechSanc.Rows) 
     { 
      for (int i = 0; i < columnCount; i++) 
      { 
       if (!Convert.IsDBNull(dr[i])) 
       { 
        sw.Write(dr[i].ToString()); 
       } 
       if (i < columnCount - 1) 
       { 
        sw.Write(","); 
       } 
      } 
      sw.Write(sw.NewLine); 
     } 
     sw.Close(); 

     z.Add(tmpPath, tmpFileName); 
     z.CommitUpdate(); 

    } 

    //Fund Allocation 
    System.Data.DataTable dtFA = blDbFunction.GetFundAllocationTS(ddlAgency.SelectedValue.ToString(), ddlDivision.SelectedValue.ToString(), ddlDistrict.SelectedValue.ToString(), ddlMC.SelectedValue.ToString(), ddlScheme.SelectedValue.ToString(), ddlUserType.SelectedValue.ToString(), ddlWorkType.SelectedValue.ToString(), ddlWorkNature.SelectedValue.ToString(), ddlWorkStatus.SelectedValue.ToString(), ((prpSessionData)(Session["sUserInfo"])).UId); 
    if (dtFA.Rows.Count > 0) 
    { 
     z.BeginUpdate(); 
     string tmpPath = Server.MapPath("FileTemplates"); 
     string tmpFileName = "FundsAllocationRevision.csv"; 
     tmpPath = @"" + tmpPath.Replace("/", "\\").Replace("\\Department", "") + "\\" + tmpFileName; 
     dtFA.Columns["FAId"].ColumnName = "Id"; 
     dtFA.Columns["WorkId"].ColumnName = "WorkCode"; 
     StreamWriter sw = new StreamWriter(tmpPath, false); 
     int columnCount = dtFA.Columns.Count; 
     for (int i = 0; i < columnCount; i++) 
     { 
      sw.Write(dtFA.Columns[i]); 
      if (i < columnCount - 1) 
      { 
       sw.Write(","); 
      } 
     } 
     sw.Write(sw.NewLine); 
     foreach (DataRow dr in dtFA.Rows) 
     { 
      for (int i = 0; i < columnCount; i++) 
      { 
       if (!Convert.IsDBNull(dr[i])) 
       { 
        sw.Write(dr[i].ToString()); 
       } 
       if (i < columnCount - 1) 
       { 
        sw.Write(","); 
       } 
      } 
      sw.Write(sw.NewLine); 
     } 
     sw.Close(); 

     z.Add(tmpPath, tmpFileName); 
     z.CommitUpdate(); 
     z.Close(); 
    } 
     System.IO.FileInfo file = new System.IO.FileInfo(paths); 
     Response.ContentType = "application/text"; 
     Response.AddHeader("Content-Disposition", "attachment;filename=\"" + Path.GetFileName(paths)); 
     Response.ContentType = "application/octet-stream"; 
     Response.WriteFile(file.FullName);/// error shows here 
     Response.End(); 
} 
} 
+0

두 명의 사용자가 동시에이 작업을 시작하면 (결과는 1 초 이내) 결과 zip 파일의 이름이 같습니다. 그것이 이유 일 수 있습니다. 하지만 그런 일이 발생하면 먼저 예외를 던져야한다고 생각합니다 ... – user1429080

답변

0

나는이 당신을 돕거나하지 않습니다 확실하지 오전하지만 난 코드 아래 사용하여 우편을 전송하고, 그것을 시도하십시오. 지난 2 년간 완벽하게 사용되었습니다.

 Response.ContentType = "application/zip"; 
     Response.AppendHeader("Content-Disposition", "attachment; filename=Test.zip") 
     Response.TransmitFile(@"C:\Test.zip"); 
     Response.Flush(); 
     // Prevents any other content from being sent to the browser 
     Response.SuppressContent = true; 
     // Directs the thread to finish, bypassing additional processing 
     HttpContext.Current.ApplicationInstance.CompleteRequest();