8

Trying to send simple text string as a file, that being downloaded under certain name upon request. Can't seems to figure out why this code fails.

var text_ready = "This is a content of a txt file."
res.setHeader('Content-type', "application/octet-stream");
res.setHeader('Content-disposition', 'attachment; filename=file.txt');
res.send( new Buffer(text_ready) );

When this code executes, I receive only a XHR response (with that string as a content), but no download is being initiated. But I expected that receiveing this response will force browser to download a file with file.txt as a name having content of the string above.

How to fix that? What am I doing wrong?

Maybe it will be important: working under Chrome 41 on Windows.

EDIT: it seems that I have to describe a bit deeper. The workflow is the following:

  • page contains a table cells with angular ng-click events attached to each of them
  • when clicking a GET request is being sent to "/download" using jQuery's $.get("/download")
  • server have route to handle this GET requests
  • what I need to achieve is that a certain text string is being send to user and saved as a file (so that a download is being initiated upon click, shortly speaking)

testings

  1. It works when I navigate manually to that url by entering this url to address bar and pressing enter
  2. It does not work when I click with mouse on that cell - the request is sent, the response is received but no download is initiated.
kaytrance
  • 2,657
  • 4
  • 30
  • 49

1 Answers1

14

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 that you're not running the latest version of node.js. Maybe you can change only Content-type header, which might work. Also, you can use res.end( new Buffer(text_ready) );. I've tested it.

EDITED: How to download file from node.js with javascript(jQuery onclick) call:

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 from jQuery to angularJS. :) I think that this is better than using an Ajax call. If you really need Ajax, I'll find out how to do it.

Ian Grainger
  • 5,148
  • 3
  • 46
  • 72
Kristiyan
  • 1,655
  • 14
  • 17
  • I have node 0.11.30 and express 4.3.2. Still doesn't work. Can this be due to that this is a POST request using jQuery (not the GET one)? – kaytrance Apr 10 '15 at 08:36
  • 2
    You can't download file with ajax call. Is it possible to use iframe? Somethink like `` Or try with this: http://stackoverflow.com/questions/20830309/download-file-using-an-ajax-request – Kristiyan Apr 10 '15 at 08:56
  • I have edited initial question to describe better what I am trying to do. As I undestand the problem is due to ajax.. – kaytrance Apr 10 '15 at 08:58
  • 2
    got it working using `window.location = "/download/"`, but I will mark your answer as acceptable anyway. – kaytrance Apr 10 '15 at 09:19