Greetings,
I have a binary (zip) file I would like to send to a browser. The browser already knows the file name. I would like the browser to provide a save-as dialog defaulting to the name it already knows. I have everything working except that the file being sent and the file received don't end up the same length. The resulting file is a lot larger (binary encoding?) and is corrupt. Here is what I am using on the node/express side:
var options = {
// root: __dirname + '/' + downloadPath + '/',
dotfiles: 'deny',
headers: {
'x-timestamp': Date.now(),
'x-sent': true
}
};
fullZipPath = downloadPath + '/' + fileName;
var absPath = path.resolve(fullZipPath);
res.attachment(absPath);
res.setHeader('Content-Disposition', 'attachment');
res.set('Content-Type', 'application/octet-stream');
res.sendFile(absPath, options, function (err) {
if (err) {
console.log(err);
res.status(err.status).end();
} else {
console.log('Sent:', absPath);
}
});
Here is what I have on the browser side:
var downloadStream = function (filename, data) {
var pom = document.createElement('a');
pom.setAttribute('href', 'data:application/zip;charset=utf-8,' + encodeURIComponent(data));
pom.setAttribute('download', filename);
if (document.createEvent) {
var event = document.createEvent('MouseEvents');
event.initEvent('click', true, true);
pom.dispatchEvent(event);
} else {
pom.click();
}
}
I really appreciate the help. I've been working on this for a whole day already.
Thanks.
Blake McBride