locked
How to save a zipfile to disk RRS feed

  • Question

  • User-1449468620 posted

    I am trying to access the zipFile response from the client side using Angular. I am having trouble figuring this out so I was wondering if it would be easier to save the zipfile to disk. I understand very little with streams. I see that the zip has been created but I do not know how to access it from angular. How do I save the file to a specific folder? I was able to save it using a basic file saving method. The .pdfs are inside it but they are corrupted. I assume because it is from a stream and I am missing some steps.

    Angular Controller

    $scope.downloadZip = function (model) {
        var model = $scope.selection;
        var promise = DocumentZip.save(model);
    
        promise.$promise.then(function success(model) {
            notificationFactory.success();
    
        });
    }

    ApiController

     [HttpPost]
        [ActionName("ZipFileAction")]
        public HttpResponseMessage ZipFiles([FromBody]int[] id)
        {
            if (id == null)
            {//Required IDs were not provided
                throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.BadRequest));
            }
    
            List<Document> documents = new List<Document>();
            using (var context = new ApplicationDbContext())
            {
                foreach (int NextDocument in id)
                {
                    Document document = context.Documents.Find(NextDocument);
    
                    if (document == null)
                    {
                        throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound));
                    }
    
                    documents.Add(document);
                }
                var streamContent = new PushStreamContent((outputStream, httpContext, transportContent) =>
                {
                    try
                    {
                        using (var zipFile = new ZipFile())
                        {
                            foreach (var d in documents)
                            {
                                var dt = d.DocumentDate.ToString("y").Replace('/', '-').Replace(':', '-');
                                string fileName = String.Format("{0}-{1}-{2}.pdf", dt, d.PipeName, d.LocationAb);
                                zipFile.AddEntry(fileName, d.DocumentUrl);
                            }
                            zipFile.Save(outputStream); //Null Reference Exception
                        }
                    }
                    finally
                    {
                        outputStream.Close();
                    }
                });
                 streamContent.Headers.ContentType = new MediaTypeHeaderValue("application/zip");
                 streamContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
                 streamContent.Headers.ContentDisposition.FileName = "reports.zip";
    
                var response = new HttpResponseMessage(HttpStatusCode.OK)
                {
                    Content = streamContent
                };
                return response;
            }
        }

    Here is how I tried to save the zipfile instead of returning it in a response. It saves the zip with no errors but it is corrupted.

    public HttpResponseMessage ZipFiles([FromBody]int[] id)
            {
                if (id == null)
                {//Required IDs were not provided
                    throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.BadRequest));
                }
    
                List<Document> documents = new List<Document>();
                using (var context = new ApplicationDbContext())
                {
                    foreach (int NextDocument in id)
                    {
                        Document document = context.Documents.Find(NextDocument);
    
                        if (document == null)
                        {
                            throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound));
                        }
    
                        documents.Add(document);
                    }
    
                    using (var zipFile = new ZipFile())
                    {
                        foreach (var d in documents)
                        {
                            var dt = d.DocumentDate.ToString("y").Replace('/', '-').Replace(':', '-');
                            string fileName = String.Format("{0}-{1}-{2}.pdf", dt, d.PipeName, d.LocationAb);
                            zipFile.AddEntry(fileName, d.DocumentUrl);
                        }
                        var zipName = "reports.zip";
                        string path = Path.Combine(HttpContext.Current.Server.MapPath("~/ZipFiles/"), Path.GetFileName(zipName));
                        zipFile.Save(path); //Null Reference Exception
                    }
                   
                    return new HttpResponseMessage(HttpStatusCode.OK);
                }
            }

    Here is a screenshot of the fiddler output. not sure if it helps

    Screenshot

    Monday, April 6, 2015 7:48 AM

All replies