Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Getting error 'The compressed(zipped) folder is invalid' while receiving zip file from web api on angularjs. I am creating a zip file at web api and sending the file to angularjs POST method call.
The angular solution receives the data but on using a BLOB object and saving it it is saved as 1 KB file and the file is corrupted.

What I have tried:

Web Api code:
[HttpPost]
        [ODataRoute("DownloadGalleryzipfile")]
        public IHttpActionResult DownloadGalleryzipfile(string type, string galleryIds)
        {
            appLogService.WriteLog("MapService.Service", "MapServiceController", "DownloadGalleryzipfile", "DownloadGalleryzipfile method is executing");
            List<int> lstIds = galleryIds.Split(',').ToList().ConvertAll<int>(Convert.ToInt32); ;
            var result = this.mapServiceDomain.GetSpecificGalleryImagesVideos(type, lstIds.ToArray());
            if (result!= null & result.Count>0)
            {
                using (ZipFile zip = new ZipFile())
                {
                    foreach(var galleryDetail in result)
                    {
                        zip.AddEntry(galleryDetail.Name, Convert.FromBase64String(galleryDetail.ImageStream));//Zip file inside filename 
                    }
                  
                    var pushStreamContent = new PushStreamContent((stream, content, context) =>
                    {
                        zip.Save(stream);
                        stream.Close();
                    }, "application/zip");

                    HttpResponseMessage httpResponseMessage = new HttpResponseMessage(HttpStatusCode.OK)
                    {
                        Content = pushStreamContent
                    };
                    httpResponseMessage.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
                    {
                        FileName = result.FirstOrDefault().CityNm+ type + "Gallery.zip"
                    };
                    httpResponseMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("application/zip");
                    httpResponseMessage.Headers.AcceptRanges.Add("bytes");
                    ResponseMessageResult responseMessageResult = ResponseMessage(httpResponseMessage);
                    return responseMessageResult;
                }
            }
            else
            {
                HttpResponseMessage httpResponseMessage = new HttpResponseMessage(HttpStatusCode.ExpectationFailed);
                ResponseMessageResult responseMessageResult = ResponseMessage(httpResponseMessage);
                return responseMessageResult;
            }
          
        }



Angular side ajax call;


$http({
                method: 'POST',
                url: dcsoConfig.DownloadGalleryzipfile + 'Images&galleryIds=' + downloadImageIds,
                responseType: 'arraybuffer',
                ContentType: 'application/json',
                Accept: 'application/zip',
                cache: false

            }).then(
            function(data, status, headers, config) {
                var a = document.createElement('a');
                var blob = new Blob([data], { 'type': "application/zip" });
                a.href = URL.createObjectURL(blob);
                a.download = "filename.zip";
                a.click();
            });
Posted
Updated 7-May-18 3:08am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900