I'm trying to download a file from Node.js server and getting the download blocked by Chrome. After searching for the solution, I came across a similar question.
I added a "Access-Control-Allow-Origin", "*"
to the header but still getting download blocked.
The client-side code:
onClickLink: async function(oEvent) {
const oModel = this.getView().getModel();
const responseData = await getDocumentFile(documentFileName);
let a = document.createElement("a");
a.href = responseData;
a.download = "file.docx";
a.click()
},
function getDocumentFile(fileName) {
const ip = location.host;
let request = $.ajax({
type: "POST",
url: http() + ip + "/documentGetFile",
responseType: "arraybuffer",
headers: {
"Accept": "application/docx"
},
data: {
fileName: fileName
},
error: function (err) {
console.log("ERROR: " + err);
}
});
return request;
}
The server-side:
app.post("/documentGetFile", async function (req, res, next) {
const fileName = req.body.fileName;
const downloadResult = await downloadFileFromAPI(fileName,
CUSTOMER_CONFIG.default.data.fileFolder);
// downloadResult.file returns a 15MB file
res.setHeader("Content-Type", "application/docx");
res.setHeader("Accept-Encoding", "base64");
res.setHeader("Access-Control-Allow-Origin", "*");
res.send(downloadResult.file);
res.end();
});