Datatable to csv write to zip下载错误:因为它正被另一个进程使用



在第一段代码中,我有两个数据库选项卡,并在datatable中提取数据,然后在流写入器的帮助下将所有数据写入csv文件,然后将两个文件添加到文件夹中并以zip形式下载,但有时会显示错误进程无法访问文件'xxxxx.zip',因为它正被另一个进程使用

  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();
 }
}

我不确定这是否会对你有所帮助,但我正在使用下面的代码传输zip,请尝试一下。它运行得很好,在过去的两年里一直在使用。

        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();

相关内容

最新更新