将多个文档下载为zip文件(Ajax调用)不起作用

时间:2018-11-06 08:48:13

标签: c# jquery ajax wcf asp.net-ajax

我正在尝试下载从WCF(C#)检索的多个文档,并将其传递通过data.Stream。显然可以,但是我无法打开下载的zip,有人可以帮我吗?

   downloadSelectedItems: function () {
                var selectedDocumentsUrl = [];
                for (var i = 0; i < datatableResult.selected.length; i++) {
                    selectedDocumentsUrl.push(datatableResult.selected[i]["relativeUrlToDocument"]);
                }
                var digest = $('#__REQUESTDIGEST').val();
                $.ajax({
                    type: "POST",
                    url: _spPageContextInfo.webAbsoluteUrl + "/_vti_bin/Infratel.Documentale/_wcf/Documentale.svc/DownloadDocuments",
                    headers: {
                        'Content-Type': 'application/json',
                        "X-RequestDigest": digest
                    },
                    data: JSON.stringify({
                        "selectedDocumentsUrl": selectedDocumentsUrl
                    }),        
                    contentType: 'application/zip',
                    dataType: 'json',
                    responseType: 'arraybuffer'
                }).done(function (data, textStatus, jqXHR) {
                var blob = new Blob([JSON.stringify(data.Stream, null, 2)], { type: 'application/zip' });
                var link = document.createElement('a');
                var url = window.URL || window.webkitURL;
                link.href = url.createObjectURL(blob);
                link.download = "ArchiveZip";
                document.body.appendChild(link);
                link.click();
                document.body.removeChild(link);
            });                    
        }

 public Result DownloadDocuments(DataContract data)
        {
            Result result = new Result { Outcome = OutComeType.KO };

                using (SPSite site = new SPSite(SPContext.Current.Site.Url))
                {
                    var rootWeb = site.RootWeb;
                    using (MemoryStream stream = new MemoryStream())
                    {
                        using (var archive = new ZipArchive(stream, ZipArchiveMode.Create, true))
                        {
                            foreach (var fileUrl in data.selectedDocumentsUrl)
                            {
                                var item = rootWeb.GetListItem(fileUrl);
                                var demoFile = archive.CreateEntry(item.File.Name);

                                using (var entryStream = demoFile.Open())
                                {
                                    item.File.OpenBinaryStream().CopyTo(entryStream);
                                }
                            }
                            //string filename = DateTime.Now.ToFileTime().ToString() + ".zip";
                            //HttpContext.Current.Response.Clear();
                            //HttpContext.Current.Response.ClearHeaders();
                            //HttpContext.Current.Response.ClearContent();
                            //HttpContext.Current.Response.AddHeader("Content-Length", stream.Length.ToString());
                            //HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);
                            //HttpContext.Current.Response.ContentType = "application/zip";                       
                            //HttpContext.Current.Response.End();

                            //HttpContext.Current.Response.TransmitFile(filename);
                            //byte[] buffer = new byte[65536];
                            //stream.Position = 0;
                            //int num;
                            //do
                            //{
                            //    num = stream.Read(buffer, 0, buffer.Length);
                            //    HttpContext.Current.Response.OutputStream.Write(buffer, 0, num);
                            //}

                            //while (num > 0);
                            //HttpContext.Current.Response.Flush();
                            stream.Position = 0;
                            result.Stream = stream;
                        }           
                    }

                }
                result.Outcome = OutComeType.OK;

            return result;
        }

0 个答案:

没有答案