如何在 SAPUI5 的浏览器中压缩所有下载的文件

How to zip all downloaded files in the browser in SAPUI5

我发现 this article 正确地解释了如何创建作者使用以下函数创建的“Hello.txt”文件的 zip 文件的工作方式(我对其进行了测试):

onPress: function(oControlEvent) {
   var zip = new JSZip();
   zip.file("Hello.txt", "Hello World\n"); //Here creates the file and the context
   zip.generateAsync({
      type: "blob"
   })
   .then(function(content) {
      saveAs(content, "example.zip");
   });
},

我有以下功能可以成功打开一个新的 window 并下载所有选定的文件:

downloadAttachments:function(){
  this.byId("UploadCollection").getSelectedItems().map(object => window.open(object.mProperties.url));
},

如何使 zip 文件包含所有下载内容而不是“Hello.txt”文件?

只需遍历您的文件并为所有文件调用 zip.file(...)

var files = ["file1", "file2", "file3"];
var zip = new JSZip();

files.forEach((file) => {
    zip.file(file, "some content based on what you want to add");
});

zip.generateAsync({
    type: "blob"
}).then(function(content) {
    saveAs(content, "example.zip");
});

您可以阅读更多 JSZip 文档 here。在您的情况下,您可以迭代所选项目并从中提取您需要的任何内容以包含在 zip 中。

无法在 SAPUI5 中压缩文件。原因是 JSUtils 库在 SAPUI5 中没有按预期工作。我认为这样做的主要原因是它是基于 Promise 的。

注意:Promises 不适用于 SAPUI5 中的 oData 服务,因为它只接受批处理调用或异步。