0

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);
3
  • Can you try by adding a content-length to the Response? response.AddHeader("Content-Length", ms.Length); right before ms.Position = 0;
    – Evariste
    Commented Dec 14, 2015 at 15:49
  • @Evariste: Thank you for your comment. I update code by your suggestion and it works. Could you please write the answer and explain about this? Appreciateyour help much.
    – hoanvd1210
    Commented Dec 15, 2015 at 4:32
  • Thanks for the feedback, I added my comment as an answer.
    – Evariste
    Commented Dec 15, 2015 at 6:39

1 Answer 1

1

Can you try by adding a content-length to the Response?

response.AddHeader("Content-Length", ms.Length);

right before ms.Position = 0;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.