Tested on my node.js version 0.12.2, Windows 7, Chrome and Firefox
var http = require('http'); http.createServer(function (req, res) { var text_ready = "This is a content of a txt file." res.writeHead(200, {'Content-Type': 'application/force-download','Content-disposition':'attachment; filename=file.txt'}); res.end( text_ready ); }).listen(8080, '127.0.0.1'); console.log('Server running at http://127.0.0.1:8080/');
I think you are not using the latest version of node.js. Perhaps you can change the Content-type header, which may work. Alternatively, you can use res.end( new Buffer(text_ready) ); . I tested it.
Editorial: How to download a file from node.js with javascript call (jQuery onclick):
JavaScript:
<script type="text/javascript"> $(document).ready(function(){ $("#button").on('click',function(){ window.location = 'http://127.0.0.1:1337/'; }); }); </script>
HTML:
<button id="button">Download</button>
I'm sure you know how to rewrite jQuery to angularJS. :) I think this is better than using an Ajax call. If you really need Ajax, I will learn how to do it.
source share