Hi Everyone,
In my project I do need to download multiple attachment as a zip file. My attachments are in the Hana db as Largebinary with mediaType annotation. I'm using adm-zip to zipping files in nodejs server side. I am able to download zip file on my pc but I can't open it. But when I tried to save this zip file on disk, I can open this zip correctly.
Is there any trick or best practice on CAP to download there custom binaries?
I have an action in my CDS service side
action downloadDocs(earchiveIDs : array of String,type : String) returns DisplayFileResponse;<br>
On the backend of this CDS
this.on(["downloadDocs"], EArchiveFiles, async (req, next) => {
var Ids = req.data.earchiveIDs;
var type = req.data.type;
if (!Ids) {
throw new Exception("There Are Not Any Ids");
}
var datas = [];
if (type == "xml")
datas = await SELECT.from(EArchiveFiles).where({ EARCHIVE_ID: { in: Ids }, and: { MEDIATYPE: { "=": "application/xml" } } });
else
datas = await SELECT.from(EArchiveFiles).where({ EARCHIVE_ID: { in: Ids }, and: { MEDIATYPE: { "!=": "application/xml" } } });
if (!datas) { throw new Exception("No data found"); }
var zip = new AdmZip();
datas.forEach(file => {
zip.addFile(file.fileName, file.blob, "comment here");
});
var buffer = zip.toBuffer();
return {
contentType: 'application/zip',
contentBlob: encodeBase64(buffer.toString('base64'))
};
})<br>
UI5/fiori Side
oOperation.execute().then(() => {
let response = oOperation.getBoundContext().getObject();
var decodedDoc = atob(response.contentBlob);
var byteArray = new Uint8Array(decodedDoc.length)
for (var i = 0; i < decodedDoc.length; i++) {
byteArray[i] = decodedDoc.charCodeAt(i);
}
var blob = new Blob([byteArray.buffer], { type: response.contentType });
var _pdfurl = URL.createObjectURL(blob, { type: response.contentType });
let link = document.createElement('a');
link.href = _pdfurl;
link.download = "Test";
link.click();
})