Following on from this question, I cannot get the browser to initiate a download from express. Backend code:
app.get('/download', (req, res) => {
res.download('./textfile.txt', (err) => {
if (err) {
console.log('error: ' + err);
} else {
console.log('success');
}
});
})
I have changed from a .gpx file to text file to ensure its nothing to do with that, and I have tried playing with the headers as below, to no avail:
res.header('Content-Type', 'text/plain')
res.header('Content-Security-Policy', 'upgrade-insecure-requests');
On the front-end I have tried:
window.location.href = 'localhost:3000/download';
and:
const filePath = 'localhost:3000/download';
const link = document.createElement('a');
link.href = filePath;
link.download = filePath.substr(filePath.lastIndexOf('/') + 1);
link.click();
and:
const newWindow = window.open('localhost:3000/download', 'download');
and navigating to the backend url via GET request (per response to my original question), but none of them initiate the download in the browser.
I think it is a front-end problem, as when I double-click on the download in the console, it opens. The front end is getting the data, but the browser (chrome) is not downloading it. I also tried Firefox, same result.
There are many queries similar to this, but none have solved the issue for me.