On my MVC project I have an AJAX call to a Web API.
I send an array of documents' routes, the API (should) zips them and returns the zip file.
self.zipDocs = function (docs, callback) {
$.ajax({
url: "../SharedAPI/documents/zip",
type: "POST",
data: docs,
contentType: "application/json",
success: function (data) {
var zip = new JSZip(data);
var content = zip.generate({ type: "blob" });
saveAs(content, "example.zip");
},
error: function (data) {
callback(data);
}
});
}
And my ZipDocs function on the WebAPI (using the DotNetZip library):
[HttpPost]
[Route("documents/zip")]
public HttpResponseMessage ZipDocs([FromBody] string[] docs)
{
using (var zipFile = new ZipFile())
{
zipFile.AddFiles(docs, false, "");
return ZipContentResult(zipFile);
}
}
protected HttpResponseMessage ZipContentResult(ZipFile zipFile)
{
// inspired from http://stackoverflow.com/a/16171977/92756
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 };
}
But when the Zip is returned I got the following error:
Uncaught Error: Corrupted zip: missing 16053 bytes.
What is really weird, when I save on the API the zip file to the disk it gets saved properly and I can open the file without any problem!
What am I doing wrong? am I missing something? Please help!
Thanks in advance.