Quantcast
Channel: Web API
Viewing all articles
Browse latest Browse all 4850
↧

How to create a zip file with Web Api

$
0
0

Right now I am able to download one pdf at a time. Here is what I use for that

<a href="{{model.DocumentUrl}}" download><button>Download</button></a>

So now I have added check boxes to the table to select the pdfs i want to zip and download.

<input id="{{model.DocumentId}}" type="checkbox" value="{{model.DocumentId}}" ng-checked="selection.indexOf(model.DocumentId) > -1" ng-click="toggleSelection(model.DocumentId)" />

and the button to trigger the function

<button ng-click="downloadZip()" class=" btn btn-danger">Download All</button>

Angular Controller

$scope.selection = [];
// toggle selection for a given employee by name$scope.toggleSelection = function toggleSelection(DocumentId) {
    var idx = $scope.selection.indexOf(DocumentId);

    // is currently selected
    if (idx > -1) {$scope.selection.splice(idx, 1);
    }

        // is newly selected
    else {
        $scope.selection.push(DocumentId);
    }
    console.log($scope.selection)
};$scope.downloadZip = function () {$http.get('/api/apiZipDownload/' + $scope.selection)
        .success(function (data, status) {
            console.log(data)
        }).error(function (err, result) {

            console.log(err, result);
        });
}

I found a code sample online for creating the zip file but i have no idea how to use it. For the Get call I am using a method from another controller that retrieves one document at a time by the id. So I have changed the (int id) to (string id). A break point shows the id's are making it from the client side but then it errors out. I do not know how to handle this. for the single downloads i just use the DocumentUrl to retrieve it. But for the multiple pdfs do I use the DocumentId or DocumentUrl? And How do i pass this information into the Zip method? Like i said, I do not know what to do and a lot of the help online is fairly complicated for me to follow.

// GET: api/Documents/5
        [ResponseType(typeof(Document))]
        public async Task<IHttpActionResult> Get(string id)
        {
            using (var context = new ApplicationDbContext())
            {
                Document document = await context.Documents.FindAsync(id);
                if (document == null)
                {
                    return NotFound();
                }

                return Ok(document);
            }
        }


        public HttpResponseMessage Zipped()
        {
            using (var zipFile = new ZipFile())
            {
                // add all files you need from disk, database or memory
                // zipFile.AddEntry(...);

                return ZipContentResult(zipFile);
            }
        }

        protected HttpResponseMessage ZipContentResult(ZipFile zipFile)
        {
            
            var pushStreamContent = new PushStreamContent((stream, content, context) =>
            {
                zipFile.Save(stream);
                stream.Close(); // After save we close the stream to signal that we are done writing.
            }, "application/zip");

            return new HttpResponseMessage(HttpStatusCode.OK) { Content = pushStreamContent };
        }

 

↧

Viewing all articles
Browse latest Browse all 4850


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>