C# C提示ASP中打开/保存zip接收错误
首先让我说,我确信这是一件非常简单的事情,不幸的是,我似乎无法理解它。所以我的问题是,我查询数据库,返回我需要的内容,将其全部压缩,并提示用户保存。当提示出现时,它将显示System.IO.Compression.ZipArchive.Zip文件名。当您试图打开此文件时,它表示该文件无效。任何帮助都将不胜感激C# C提示ASP中打开/保存zip接收错误,c#,asp.net,zip,C#,Asp.net,Zip,首先让我说,我确信这是一件非常简单的事情,不幸的是,我似乎无法理解它。所以我的问题是,我查询数据库,返回我需要的内容,将其全部压缩,并提示用户保存。当提示出现时,它将显示System.IO.Compression.ZipArchive.Zip文件名。当您试图打开此文件时,它表示该文件无效。任何帮助都将不胜感激 if (e.CommandName == "DownloadAttachment") { e.Canceled = true; // Create
if (e.CommandName == "DownloadAttachment")
{
e.Canceled = true;
// Create a zip and send it to the client.
//Response.Write(@"<script language='javascript'>alert('Details saved successfully')</script>");
var item = e.Item as GridEditableItem;
fileId = (int)item.GetDataKeyValue("Unique");
FileData[] allrecords = null;
using (
SqlConnection conn =
new SqlConnection(ConfigurationManager.ConnectionStrings["PtcDbModelEntities"].ConnectionString))
{
using (
SqlCommand comm = new SqlCommand("Select Unique1, BinaryData, FileName from PtcDbTracker.dbo.CafFileTable where Unique1=@fileId AND FileName IS NOT NULL", conn))
{
comm.Parameters.Add(new SqlParameter("@fileId", fileId));
conn.Open();
using (var reader = comm.ExecuteReader())
{
var list = new List<FileData>();
while (reader.Read())
{
list.Add(new FileData { Unique1 = reader.GetInt32(0) });
long len = reader.GetBytes(1, 0, null, 0, 0);
Byte[] buffer = new byte[len];
list.Add(new FileData { BinaryData = (byte)reader.GetBytes(1, 0, buffer, 0, (int)len), FileName = reader.GetString(2) });
allrecords = list.ToArray();
}
}
conn.Close();
}
}
using (var compressedFileStream = new MemoryStream())
{
//Create an archive and store the stream in memory.
using (var zipArchive = new ZipArchive(compressedFileStream, ZipArchiveMode.Update, false))
{
if (allrecords != null)
{
foreach (var record in allrecords)
{
//Create a zip entry for each attachment
if (record.FileName != null)
{
var zipEntry = zipArchive.CreateEntry(record.FileName);
//Get the stream of the attachment
using (var originalFileStream = new MemoryStream(record.BinaryData))
{
using (var zipEntryStream = zipEntry.Open())
{
//Copy the attachment stream to the zip entry stream
originalFileStream.CopyTo(zipEntryStream);
}
}
}
}
}
Response.ClearContent();
Response.ClearHeaders();
Response.BinaryWrite(compressedFileStream.ToArray());
Response.AppendHeader("Content-Disposition", "Attachment; filename=result.zip");
Response.Flush();
Response.Close();
zipArchive.Dispose();
//How Do I Prompt for open or save?
}
}
编辑:包括萨米的建议。Zip文件夹现在可以保存和打开,但没有任何内容。您可以发送正确的内容处置头,但对于文件名,您只需附加对象,因此平台会在其上调用ToString,默认情况下,它会输出类名。您可能希望发送随机文件名或静态文件名
Response.AppendHeader("Content-Disposition", "Attachment; filename=result.zip");
然后发送文件。您使用MemoryStream来保存zip归档文件,而不发送它。一种方法是
Response.BinaryWrite(compressedFileStream.ToArray());
Response.Flush();
Response.Close();
能否请您澄清您正在寻找的帮助-文件名不确定是否有问题,听起来您知道原因/预期的行为,文件损坏似乎也是预期的,因为您没有写入任何输出,或者我如何提示打开或保存?或者是完全不同的东西。对不起,Alexei,我有点陷入了这种境地,所以我最后写的每一段代码都是一次新的冒险。我知道response.appendheader将提示打开/保存对话框。我如何将zip放入输出流中,以便成功下载我创建的zip文件,以及如何为文件命名?您提到的每个问题都有多个答案,因此。。。如果你一次只关注一个问题,有人可能会为你提供答案或搜索重复链接。“到目前为止感觉太宽泛了。”阿列克谢列文科夫我理解你的沮丧,我已经找到了。我不喜欢在没有必要的情况下发帖,我不想感觉别人在做我的工作。我已经对原始帖子进行了编辑。现在,我的问题似乎是文件夹中没有任何内容,尽管我一步一步走过去,它看起来像是在写东西。我已经做了编辑,请参见OP。现在文件夹打开了,只是没有内容。@MatthewDetweiler如果你有其他问题,请将其作为单独的问题发布,不要编辑原始帖子/代码,从那时起,答案将与当前问题不同步。