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 | Show 1 more comment1 Answer
Reset to default 1I 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
版权声明:本文标题:c# - creating Zip file on the Download folder of User profile fails - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741647592a2390278.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
Path.GetExtension(file).ToUpper() != ".ZIP"
will fail on turkish systems. Better useEquals(".zip", StringComparison.OrdinalIgnoreCase)
– Klaus Gütter Commented Feb 11 at 17:49