admin管理员组

文章数量:1297033

I am trying to make a zip file and download on the Download Folder of the User profile

public void CreateZipFile(string tempFolderPath, string zipFilePath, string zipFileName)
{     
  using (Package zip = Package.Open(zipFilePath, FileMode.Create))
  {
      foreach (string file in Directory.GetFiles(tempFolderPath))
      {
          if (Path.GetExtension(file).ToUpper() != ".ZIP")
          {
              string zipPartUri = $"/{Uri.UnescapeDataString(Path.GetFileName(file))}";

              Uri partUri = PackUriHelper.CreatePartUri(new Uri(zipPartUri, UriKind.Relative));
              PackagePart part = zip.CreatePart(partUri, System.Net.Mime.MediaTypeNames.Application.Octet, CompressionOption.Normal);

              using (FileStream fileStream = new FileStream(file, FileMode.Open, FileAccess.Read))
              {
                  using (Stream zipPartStream = part.GetStream())
                  {
                      fileStream.CopyTo(zipPartStream);
                  }
              }
          }

      }
  }

  try
  {
      HttpContext.Current.Response.Clear();
      HttpContext.Current.Response.ContentType = "application/zip";
      HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + zipFileName);
      HttpContext.Current.Response.TransmitFile(zipFilePath);
      HttpContext.Current.Response.Flush();
  }
  catch (Exception ex)
  {
      // Log the exception (optional)
      Console.WriteLine("Error sending ZIP file: " + ex.Message);
  }
  finally
  {
      
      HttpContext.Current.ApplicationInstance.CompleteRequest(); // Graceful request termination
  }

}

zipFileName = "GroupFiles.zip" zipFilePath = C:\Temp\1\GroupedExcelFiles.zip

The zip file is created on the C:\Temp\1\ , but it can not transfer it to the download Folder. Actually it finishes this line

HttpContext.Current.ApplicationInstance.CompleteRequest(); // Graceful request termination

and starts returning the parent methods. but on the last one it gives error, which is on the localhost

how can I check the problem? ps: I am using framework 4.0 and System.IO.Packaging; for making zip file

I am trying to make a zip file and download on the Download Folder of the User profile

public void CreateZipFile(string tempFolderPath, string zipFilePath, string zipFileName)
{     
  using (Package zip = Package.Open(zipFilePath, FileMode.Create))
  {
      foreach (string file in Directory.GetFiles(tempFolderPath))
      {
          if (Path.GetExtension(file).ToUpper() != ".ZIP")
          {
              string zipPartUri = $"/{Uri.UnescapeDataString(Path.GetFileName(file))}";

              Uri partUri = PackUriHelper.CreatePartUri(new Uri(zipPartUri, UriKind.Relative));
              PackagePart part = zip.CreatePart(partUri, System.Net.Mime.MediaTypeNames.Application.Octet, CompressionOption.Normal);

              using (FileStream fileStream = new FileStream(file, FileMode.Open, FileAccess.Read))
              {
                  using (Stream zipPartStream = part.GetStream())
                  {
                      fileStream.CopyTo(zipPartStream);
                  }
              }
          }

      }
  }

  try
  {
      HttpContext.Current.Response.Clear();
      HttpContext.Current.Response.ContentType = "application/zip";
      HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + zipFileName);
      HttpContext.Current.Response.TransmitFile(zipFilePath);
      HttpContext.Current.Response.Flush();
  }
  catch (Exception ex)
  {
      // Log the exception (optional)
      Console.WriteLine("Error sending ZIP file: " + ex.Message);
  }
  finally
  {
      
      HttpContext.Current.ApplicationInstance.CompleteRequest(); // Graceful request termination
  }

}

zipFileName = "GroupFiles.zip" zipFilePath = C:\Temp\1\GroupedExcelFiles.zip

The zip file is created on the C:\Temp\1\ , but it can not transfer it to the download Folder. Actually it finishes this line

HttpContext.Current.ApplicationInstance.CompleteRequest(); // Graceful request termination

and starts returning the parent methods. but on the last one it gives error, which is on the localhost

how can I check the problem? ps: I am using framework 4.0 and System.IO.Packaging; for making zip file

Share Improve this question edited Feb 11 at 16:39 nnmmss asked Feb 11 at 16:28 nnmmssnnmmss 3,0029 gold badges47 silver badges74 bronze badges 6
  • BTW Path.GetExtension(file).ToUpper() != ".ZIP" will fail on turkish systems. Better use Equals(".zip", StringComparison.OrdinalIgnoreCase) – Klaus Gütter Commented Feb 11 at 17:49
  • "Packaging" is for "objects" (whatever that means). I use a Standard library and compression. A "Standard" lib because you can't "interop" UWP and Console otherwise; for example. – Gerry Schmitz Commented Feb 11 at 19:12
  • I created a simple demo to test your code. In fact, it works perfectly. This also shows that there is no issue with the code you uploaded; the problem lies in how you set up the data exchange process between the client and the server outside of that code. I found two very similar cases; you can take a look and see if it sparks any insights. stackoverflow/questions/1554728/… learn.microsoft/en-us/answers/questions/1063422/… – Zhenning Zhang Commented Feb 12 at 8:06
  • @KlausGütter : that's not the problem – nnmmss Commented Feb 12 at 14:53
  • @ZhenningZhang: if I write it to the folder by File.WriteAllBytes(@"C:\Temp\Group.zip", result); it works, but I want it would be downloaded in Downlods Special Folder – nnmmss Commented Feb 12 at 14:55
 |  Show 1 more comment

1 Answer 1

Reset to default 1

I found it as was in the UpdatePanel, I added this code into the Page_Load

ScriptManager scriptManager = ScriptManager.GetCurrent(this.Page); scriptManager.RegisterPostBackControl(this.btnZip);

本文标签: ccreating Zip file on the Download folder of User profile failsStack Overflow