Hello, I tried to send a file from node.js to the client.
My code works, however, when the client goes to the specified URL ( /helloworld/hello.js/test), it transfers the file.
Access to it from Google Chrome makes the file (.mp3) play in the player.
My goal is to get the client browser to download the file and ask the client where he wants to save it, and not transfer it to the website.
http.createServer(function(req, res) {
switch (req.url) {
case '/helloworld/hello.js/test':
var filePath = path.join(__dirname, '/files/output.mp3');
var stat = fileSystem.statSync(filePath);
res.writeHead(200, {
'Content-Type': 'audio/mpeg',
'Content-Length': stat.size
});
var readStream = fileSystem.createReadStream(filePath);
readStream.on('open', function() {
readStream.pipe(res);
});
readStream.on('error', function(err) {
res.end(err);
});
}
});
source
share