Express: problem with res.download

Created on 13 Feb 2017  路  7Comments  路  Source: expressjs/express

Hi, in my application i need to download a file that is created inside a folder that is created at run-time, i can see from the prompt that the path is correct, but when the path is passed to res.download the folder is undefined, this isa my code:

console.log('folder',id)
var folder = id.toString()
              var file = [root,"/app/temp/",folder, "/registrazioni.zip"].join("")
              console.log('file',file)
              res.download(file,'registrazioni.zip')

this is the output of the server:

folder 1486999889561 
file /home/giuseppe/projects/csgSAW/app/temp/1486999889561/registrazioni.zip
path /home/giuseppe/projects/csgSAW/app/temp/undefined/registrazioni.zip
download done { [Error: ENOENT: no such file or directory, stat '/home/giuseppe/projects/csgSAW/app/temp/undefined/registrazioni.zip']
  errno: -2,
  code: 'ENOENT',
  syscall: 'stat',
  path: '/home/giuseppe/projects/csgSAW/app/temp/undefined/registrazioni.zip',
  expose: false,
  statusCode: 404,
  status: 404 }

I am puzzled, it is like something changes the string after is passed to res.download;
thanks in advance

4.x question

All 7 comments

try

var path = require('path');
res.download(path.resolve(file), 'registrazioni.zip')

Can you show where in the code you're logging the following at?

path /home/giuseppe/projects/csgSAW/app/temp/undefined/registrazioni.zip

Hi, with path.resolve I still get the same problem, in my code I log console.log('folder',id) that is correct
then I log console.log('file', file) this is correct too, but I do not log console.log('path,path) I think this came out from res.download

Maybe you want to see the code:
code_to_check.txt

Hi @arpho thanks for that code. I bundled it up into some app and didn't find any issues really. I still don't see that console.log('path,path) statement in your code, and it's not coming from Express, as there are no console statements in Express besides for the global error handler.

Thanks,I solved

Glad to hear, @arpho ! Feel free to share any details on what went wrong if you like, because maybe there is docs we can improve, the API we can improve, or some kind of thing to help in the future :)

Was this page helpful?
0 / 5 - 0 ratings