Need help sending a binary file

182 views
Skip to first unread message

Blake McBride

unread,
Feb 22, 2015, 6:11:36 PM2/22/15
to expre...@googlegroups.com
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


Aaron Martone

unread,
Feb 23, 2015, 2:54:50 PM2/23/15
to expre...@googlegroups.com
res.attachment(file), doesn't that set the Content-Dispo and Content-Type headers based on the file type? Can you try omitting those?
Also, can you ensure that the browser's request headers accept both application/zip and application/octet-stream?
Was the file uploaded by FTP? Did they by chance upload it as ASCII data rather than Binary in case automatic detection wasn't enabled? 
Reply all
Reply to author
Forward
0 new messages