I would like to get all specific files in Document Library and export them as a zip file.
The bellow code block works well when I call it from Page Layouts, or Web Parts, but does not work when I call under a my custom Rest service. Say the zip file is 10mb, when a hit the link to download the file on browser, it downloads nearly 10mb and suddenly throws Unknown Network Error. I believe that there is something wrong with response and buffer here, for example the response ended before the buffer flushes all contents. Just my thought. Please advise.
response.Clear();
response.BufferOutput = false;
System.Web.HttpContext c = System.Web.HttpContext.Current;
string archiveName = String.Format("archive-{0}.zip", filename);
response.ContentType = "application/zip";
response.AddHeader("content-disposition", "inline; filename=\"" + archiveName + "\"");
try
{
using (var ms = new System.IO.MemoryStream())
{
using (ZipFile zip = new ZipFile())
{
foreach (SPListItem item in items)
{
if (item.ContentType.Name != "Folder")
{
var file = item.File;
var content = file.OpenBinary(SPOpenBinaryOptions.None);
zip.AddEntry(file.Name, content);
}
}
zip.Save(ms);
}
ms.Position = 0;
var b = new byte[1024];
int n;
while ((n = ms.Read(b, 0, b.Length)) > 0)
response.OutputStream.Write(b, 0, n);
response.AddHeader("Content-Length", ms.Length);
right beforems.Position = 0;