title: How to download a pdf file through javascript?
body: <p>My javascript code makes the following AJAX request to my node.js server:</p>
  
  <pre><code>var url = '/node/download';
  var downloadRequest = new goog.net.XhrIo();
  downloadRequest.headers.set('content-type', 'application/json');
  downloadRequest.send(url);
  </code></pre>
  
  <p>My node.js server creates a pdf at the node and streams the pdf back to the client via the following code:</p>
  
  <pre><code>    var filestream = fs.createReadStream(pdfpath);                  
      res.writeHead(200, {
          'Content-disposition': 'attachment; filename=' + filename,
          "Content-Type":"application/pdf","Content-Transfer-Encoding": "binary"});
      filestream.on('data', function(chunk) {                     
          res.write(chunk);
      });
      filestream.on('end', function() {
          res.end();
      });
  </code></pre>
  
  <p>But now I am having trouble at how to receive this response back at the javascript client so that a download prompt will open to allow the user to download and save the pdf file.</p>
  
  <p>Please help!</p>
  
  <p>Thanx in advance!</p>
  
  <p>P.S. Plz also suggest any better way to implement my node's code(if there is any)</p>
  
  <p>Edit: One possible solution would be to send my request like this:</p>
  
  <pre><code>window.location.assign('/node/download');
  </code></pre>
  
  <p>This way i get the download prompt and everything works fine except that the asynchronous nature of the product is sacrificed. Is there a work around for this so that I can also retain asynchronicity?</p>