I am storing files in database in the form of bytes. We have requirement to download all attachments as zip file. Please suggest
Try this
protected void ZipDownload()
{
var list = //query for getting the files.
ZipFile zip = new ZipFile();
foreach (var file in list)
{
zip.AddEntry(file.docname, (byte[])file.doc.ToArray());
}
var zipMs = new MemoryStream();
zip.Save(zipMs);
byte[] fileData = zipMs.GetBuffer();
zipMs.Seek(0, SeekOrigin.Begin);
zipMs.Flush();
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=docs.zip ");
Response.ContentType = "application/zip";
Response.BinaryWrite(fileData);
Response.End();
}
Related
I am facing difficulties in generating more than 1 PDF's of Crystal report i-e in a loop. Basically I am creating PDF's then zipping those files and generating HTTP Response.For the sake of demonstration of code i am running loop twice here is my code.
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/zip";
Response.AppendHeader("content-disposition", "attachment; filename=Report.zip");
using (ZipFile zip = new ZipFile())
{
for (int i = 0; i < 2; i++)
{
var re = rpt.ExportToStream(ExportFormatType.PortableDocFormat);
string Name="PDF"+i+".pdf";
zip.AddEntry(Name, re);
}
zip.Save(Response.OutputStream);
}
Response.Clear();
It successfully generates the zip file but when i try to extract it i gives an error No archive found (The archive is either in unknown formate or damaged). Any help would be appreciated. Btw I followed this link Export Crystal Report to PDF in a Loop only works with first
Using of memorystreem if zip file not attached to local path.
Response.ClearContent(); Response.ClearHeaders();
Response.ContentType = "application/zip";
Response.AppendHeader("content-disposition", "attachment;
filename=Report.zip");
using (var memoryStream = new MemoryStream()) { using (var
archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
{
for (int i = 0; i < 2; i++)
{
var re = rpt.ExportToStream(ExportFormatType.PortableDocFormat);
string Name="PDF"+i+".pdf";
zip.AddEntry(Name, re);
zip.Save(memoryStream);
} } }
memoryStream.Seek(0, SeekOrigin.Begin);
memoryStream.WriteTo(Response.OutputStream);
I'm trying to use the Zip Archive library in .NET 4.5 to create a .zip file in memory of a bunch of byte[] attachments:
using (var memoryStream = new MemoryStream())
{
using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
{
string zipFilename = string.Format(#"c:\temp\{0} - {1}.zip",
"test",
System.DateTime.Now.ToString("yyyyMMddHHmm"));
using (var fileStream = new FileStream(zipFilename, FileMode.Create))
{
foreach (var attachment in attachments)
{
ZipArchiveEntry entry = archive.CreateEntry(attachment.FileName);
using (Stream ZipFile = entry.Open())
{
byte[] data = attachment.Data;
ZipFile.Write(data, 0, data.Length);
}
}
}
}
}
PdfAttachment is a class with a byte[] Data and string Filename.
My problem is twofold. Once, the zip archive is empty. 2, rather than save it to a file, I'd like to use the response outputs to download the file in the users browser, which I have tried with:
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}.zip; size={1}", "test.zip", memoryStream.Length));
Response.BinaryWrite(memoryStream);
Response.Flush();
Response.End();
I haven't been able to find many examples online, hence the vagueness.
The fileStream is never written to because it is not associated with the archive.
So the archive is being written to the memoryStream.
BinaryWrite only accepts a byte[] so use memoryStream.ToArray().
Also, Response.ContentType value is wrong.
Hi i'm working on asp net application. In this I need to Export Grid data to Excel and finally the excel file should be saved as zip file. I don't want to first save Excel file in some location then use zip functionality to fetch that file and convert it into Zip and save it. I want functionality which will directly convert Grid to Excel then Zip then finally saves it. I've seen so many forms and sites but none gave proper answer.
You can do this by dot.net Zip dll And following code will help you
gv.AllowPaging = false;
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment; filename=" + strFileName);
Response.ContentType = "application/zip";
System.IO.StringWriter sw = new System.IO.StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
gv.RenderControl(htw);
// byte[] toBytes = Encoding.ASCII.GetBytes(somestring);
MemoryStream stream = new MemoryStream();
string attachment = sw.ToString();
byte[] data = Encoding.ASCII.GetBytes(attachment);
stream.Write(data, 0, data.Length);
stream.Seek(0, SeekOrigin.Begin); // <-- must do this after writing the stream!
// File.WriteAllBytes(#"D:\Saurabh\Testing\inputpdf\saurabhhtest.xls", stream.GetBuffer());
using (ZipFile zipFile = new ZipFile())
{
zipFile.AddEntry("saurabhtest1.xls", stream);
zipFile.Save(Response.OutputStream);
}
Note: I have solved the problem myself. See the below answer.
I'm using ZipStorer to zip files in ASP.NET C# 4.0 WebForm.
After I created the Zip in MemoryStream and transmitted it using httpResponse, the client user was unable to open the file as a Zip File.
Any tips? Thanks.
Below is my code:
string text = GetLongText();
byte[] ba = Encoding.UTF8.GetBytes(text);
using (MemoryStream ms = new MemoryStream())
{
using (ZipStorer zip = ZipStorer.Create(ms, "My Zip File"))
{
zip.AddStream(ZipStorer.Compression.Deflate, "MyText.txt", new MemoryStream(ba), DateTime.Now, "My Text");
Response.Clear();
Response.AppendHeader("content-disposition", "attachment; filename=MyZip.zip");
Response.ContentType = "application/zip";
ms.WriteTo(Response.OutputStream);
Response.End();
}
}
I have solve the problem myself. Below is the codes:
string text = GetLongText();
byte[] ba = Encoding.UTF8.GetBytes(text);
using (MemoryStream ms = new MemoryStream())
{
using (ZipStorer zip = ZipStorer.Create(ms, "My Zip"))
{
zip.AddStream(ZipStorer.Compression.Deflate, "text.txt", new MemoryStream(ba), DateTime.Now, "My Text");
}
Response.AppendHeader("content-disposition", "attachment; filename=MyZip.zip");
Response.ContentType = "application/zip";
Response.BinaryWrite(ms.ToArray());
Response.End();
}
}
What I like to do is instead of storing a zip file on disk, I like to open it up from a MemoryStream.
I am looking at the documentation for DotNetZip programming example:
Note that I tweaked it slightly based on what I thought may be needed.
var ms = new MemoryStream();
using (ZipFile zip = new ZipFile())
{
zip.AddFile("ReadMe.txt");
zip.AddFile("7440-N49th.png");
zip.AddFile("2008_Annual_Report.pdf");
zip.Save(ms); // this will save the files in memory steam
}
// now what I need is for the zip file to open up so that
the user can view all the files in it. Not sure what to do next after
zip.Save(ms) for this to happen.
Try this:
public ActionResult Index()
{
var memoryStream = new MemoryStream();
using (var zip = new ZipFile())
{
zip.AddFile("ReadMe.txt");
zip.AddFile("7440-N49th.png");
zip.AddFile("2008_Annual_Report.pdf");
zip.Save(memoryStream);
}
memoryStream.Seek(0, 0);
return File(memoryStream, "application/octet-stream", "archive.zip");
}
If this is local. you will need to save the stream in to the file and call Process.Start on it.
If this is on server. Just write your ms into Response with appropriate mime type.
You'd have to send the content of the memory stream back as the response:
using (MemoryStream ms = new MemoryStream())
{
using (ZipFile zip = new ZipFile())
{
zip.AddFile("ReadMe.txt");
zip.AddFile("7440-N49th.png");
zip.AddFile("2008_Annual_Report.pdf");
zip.Save(ms); // this will save the files in memory steam
}
context.Response.ContentType = "application/zip";
context.Response.AddHeader("Content-Length", ms.Size);
context.Response.AddHeader("Content-disposition", "attachment; filename=MyZipFile.zip");
ms.Seek(0, SeekOrigin.Begin);
ms.WriteTo(context.Response.OutputStream);
}
Try creating an ActionResult a bit like this:
I'm not 100% sure about the line var fileData = ms; and i don't have access to a dev environment just now, but there should be enough for you to work it out.
public ActionResult DownloadZip()
{
using (MemoryStream ms = new MemoryStream())
{
using (ZipFile zip = new ZipFile())
{
zip.AddFile("ReadMe.txt");
zip.AddFile("7440-N49th.png");
zip.AddFile("2008_Annual_Report.pdf");
zip.Save(ms); // this will save the files in memory steam
}
byte[] fileData = ms.GetBuffer();// I think this will work. Last time I did it, I did something like this instead... Zip.CreateZip("LogPosts.csv", System.Text.Encoding.UTF8.GetBytes(csv));
var cd = new System.Net.Mime.ContentDisposition
{
FileName = "Whatever.zip",
// always prompt the user for downloading, set to true if you want
// the browser to try to show the file inline
Inline = false,
};
Response.AppendHeader("Content-Disposition", cd.ToString());
return File(fileData, "application/octet-stream");
}
}
this way we can write zip to output stream. may help
ZipFile zip = new ZipFile();
List<Attachment> listattachments = email.Attachments;
int acount = attachments.Count;
for (int i = 0; i < acount; i++)
{
zip.AddEntry(attachments[i].FileName, listattachments[i].Content);
}
Response.Clear();
Response.BufferOutput = false;
string zipName = String.Format("{0}.zip", message.Headers.From.DisplayName);
Response.ContentType = "application/zip";
Response.AddHeader("content-disposition", "attachment; filename=" + zipName);
zip.Save(Response.OutputStream);
Response.End();