1

I am trying to prompt the user for file download using express's res.download but it does not seem to work and gives an error in the console.

Here is my code

router.get('/download', function(req, res){

    var file = __dirname + '/public/template.docx';
    res.download(file);

});

Which gives me the following error

ERROR SyntaxError: Unexpected token P in JSON at position 0

  • Do you have tried `res.set('Content-Type', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document');` and `res.set('Content-Disposition', 'attachment; filename=template.docx')`? Take a look at [this answer](https://stackoverflow.com/questions/7288814/download-a-file-from-nodejs-server-using-express). – Hasan Bayat Jul 02 '17 at 09:21
  • @HasanBayat still gives the same error – Mira Ekladious Jul 02 '17 at 09:58
  • Do you have tried the solution at [this answer](https://stackoverflow.com/questions/7288814/download-a-file-from-nodejs-server-using-express)? – Hasan Bayat Jul 02 '17 at 10:05
  • This solution includes using res.download which is not working for me – Mira Ekladious Jul 02 '17 at 10:13
  • Do you have tried the Content-Type header? Also try `res.end();`. – Hasan Bayat Jul 02 '17 at 10:20
  • Do you mean: `res.set('Content-Type', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'); res.set('Content-Disposition', 'attachment; filename=template.docx'); var file = __dirname + '/public/template.docx'; res.sendFile(file); res.end();` still does not work and gives this error ERROR SyntaxError: Unexpected end of JSON input – Mira Ekladious Jul 02 '17 at 10:35
  • How about Streaming? Try [this answer](https://stackoverflow.com/questions/7288814/download-a-file-from-nodejs-server-using-express) old solution not the new update then report back. – Hasan Bayat Jul 02 '17 at 10:37
  • I have tried `res.download();` with a demo.docx file and it works. create a new fresh folder with fresh expressjs installed and do the same job with only one router. – Hasan Bayat Jul 02 '17 at 11:19
  • @HasanBayat can you show me your work – Mira Ekladious Jul 02 '17 at 11:22
  • A simple Hello World with your .docx file download `const express = require('express') const app = express() app.get('/', function(req, res) { res.send('Hello World!') }) app.get('/download', function(req, res) { res.download('./demo.docx') }) app.listen(3000, function() { console.log('Example app listening on port 3000!') })`. – Hasan Bayat Jul 02 '17 at 11:23
  • You can download .docx file from [here](https://calibre-ebook.com/downloads/demos/demo.docx) – Hasan Bayat Jul 02 '17 at 11:26

1 Answers1

0

I think you're possibly doing something else wrong. Here's a functional example:

const path = require('path');
const express = require('express');
const app = express();

app.get('/download-docx', (req, res) => {
  const docPath = path.join(__dirname, 'demo.docx');
  res.download(docPath, 'demo.docx', function(err){
    if (err) {
      // if the file download fails, we throw an error
      throw err;
    }
  });
})

const listener = app.listen(process.env.PORT, () => {
  console.log('Your app is listening on port ' + listener.address().port);
});

It assumes there's a demo.docx file in the same directory the above file is in. If you'd like a completely functional example you can edit I built one for you on glitch.com. One small catch, glitch doesn't show the demo.docx file in the file tree, but rest assured it is actually there. You can confirm for yourself using the console option in the Advanced Options menu in the top left.

alextes
  • 1,817
  • 2
  • 15
  • 22