C#提示open/save zip in ASP接收错误

C# Prompt for open/save zip in ASP receiving error

首先让我说我确定这很简单,不幸的是我似乎无法弄清楚。所以这是我的问题,我查询数据库,return 我需要什么,把它全部压缩,并提示用户保存。出现提示时,它会显示 System.IO.Compression.ZipArchive.Zip 文件名。当您尝试打开它时,它说文件无效。任何帮助将不胜感激!!

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?
            }
        }

编辑:包括 Sami 的建议。 Zip 文件夹现在可以保存和打开,但没有任何内容。

您发送了正确的 Content-Disposition header,但对于文件名,您只需附加 object,因此平台会在其上调用 ToString(),默认情况下它将输出 class 名称。您可能想要发送随机文件名或静态文件名。

Response.AppendHeader("Content-Disposition", "Attachment; filename=result.zip");

然后是文件的发送。您使用 MemoryStream 来保存 zip 存档,但您没有发送它。一种方法是

Response.BinaryWrite(compressedFileStream.ToArray());
Response.Flush();
Response.Close();