SPFiles到.zip

时间:2015-12-16 13:24:54

标签: file compression httpwebresponse

随着标题的提示,我有一个SPFiles(sharepoint附件)列表,我需要压缩它并推送给用户下载。

我在这里寻找了一些例子并尝试编写代码,但却没有成功。

这是我到目前为止的最后一次尝试

 using (var stream = new MemoryStream())
        {
            using (var archive = new ZipArchive(stream, ZipArchiveMode.Create, true))
            {
                foreach (SPFile item in lstFiles)
                {
                    //string nomeEntrada = item.Substring(item.LastIndexOf("/") + 1);
                    var file = archive.CreateEntry(item.Name);
                    using (var entryStream = file.Open())
                    using (var streamWriter = new BinaryWriter(entryStream))
                    {
                        byte[] bytes = item.OpenBinary();
                        streamWriter.Write(bytes, 0, bytes.Length);
                    }
                }
            }
            //For testing only, to check if the .zip is beeing correctly generated
            using (FileStream file = new FileStream("C:\\teste\\file.zip", FileMode.Create, System.IO.FileAccess.Write))
            {
                stream.Seek(0, SeekOrigin.Begin);
                stream.CopyTo(file);
            }

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ContentType = "application/x-zip-compressed";
            HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=anexos.zip");
            HttpContext.Current.Response.BinaryWrite(stream.ToArray());
            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.Close();
            HttpContext.Current.ApplicationInstance.CompleteRequest();
            HttpContext.Current.Response.End();
        }

PS:我不想将文件保存在服务器目录中,这只是检查文件是否正常的测试

1 个答案:

答案 0 :(得分:0)

我创建了一个应用程序页面并将用户重定向到它。由于我可以在那里控制httpcontext,我编写了以下代码并且它工作得很好。该页面将文件快速传输到客户端,然后它们自行关闭。希望它有所帮助

    using (var stream = new MemoryStream())
                    {
                        using (var archive = new ZipArchive(stream, ZipArchiveMode.Create, true))
                        {
                            foreach (SPFile item in lstFiles)
                            {

                                var file = archive.CreateEntry(item.Name);
                                using (var entryStream = file.Open())
                                using (var streamWriter = new BinaryWriter(entryStream))
                                {
                                    byte[] bytes = item.OpenBinary();
                                    streamWriter.Write(bytes, 0, bytes.Length);
                                }
                            }
                        }

                        stream.Seek(0, SeekOrigin.Begin);
                        Byte[] byteArray = stream.ToArray();

                        HttpContext.Current.Response.Buffer = true;
                        HttpContext.Current.Response.Clear();
                        HttpContext.Current.Response.ClearHeaders();
                        HttpContext.Current.Response.CacheControl = "public";
                        HttpContext.Current.Response.AddHeader("Pragma", "public");
                        HttpContext.Current.Response.AddHeader("Expires", "0");
                        HttpContext.Current.Response.AddHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
                        HttpContext.Current.Response.AddHeader("Content-Description", "Report Export");
                        HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=\"Anexos.zip\"");

                        HttpContext.Current.Response.BinaryWrite(byteArray);
                        HttpContext.Current.Response.Flush(); // Sends all currently buffered output to the client.
                        HttpContext.Current.Response.SuppressContent = true;  // Gets or sets a value indicating whether to send HTTP content to the client.
                        HttpContext.Current.ApplicationInstance.CompleteRequest(); // Causes ASP.NET to bypass all events and filtering in the HTTP pipeline chain of execution and directly execute the EndRequest event.
                    }