在我的Javascript代码中,我有一个映射的文档位置数组(使用Knockout)。
var mappedArray = $.map(self.checkedDocs(), function (item) { return item.pdfloc() });
是否可以使用Javascript压缩和下载这些文档?
答案 0 :(得分:0)
我最终压缩了服务器上的文档,一旦压缩了,我就在客户端下载了它们:
self.download = function () {
var mappedArray = $.map(self.checkedDocs(), function (item) { return item.pdfloc() });
self.service.zipDocs(ko.toJSON(mappedArray), function (data) {
if (data == "Error") {
$("#modalResult").text("Error has occured, please try later.");
$("#modalConfirmation").dialog();
}
else if (data == "Success") {
$("#modalResult").text("Your documents are being download.");
$("#modalConfirmation").dialog();
}
});
}
API调用:
self.zipDocs = function (docs, callback) {
$.ajax({
url: "http://api.xxx.com/functions/zip",
type: "POST",
contentType: "application/json",
dataType: "binary",
data: docs,
processData: false,
success: function (blob) {
saveAs(blob, "ZippedDocuments.zip");
callback("Success");
},
error: function (data) {
callback("Error");
}
});
}
API c#cofr:
[HttpPost]
[Route("zip")]
public HttpResponseMessage ZipDocs([FromBody] string[] docs)
{
using (ZipFile zip = new ZipFile())
{
zip.AddFiles(docs, false, "");
return ZipContentResult(zip);
}
}
我希望它能帮助那些面临同样问题的人。