res.download from a url

517 views
Skip to first unread message

Angelo Chen

unread,
Mar 11, 2013, 6:00:44 AM3/11/13
to Express
Hi,

this works:
res.download('localfile.jpeg')

this not working:
res.download('http://sample123.com/file.jpeg')

possible to do above? reason is, in the page I have a <a href="">save
as</a>, using res.download will save the file without opening a page.

Thanks,
Angelo

greelgorke

unread,
Mar 11, 2013, 6:18:10 AM3/11/13
to expre...@googlegroups.com
res.download is for local files. it does not a redirection or something like that. if you want to proxy than you have to do something like that in your route:

app.get('some path', function(req,res,next){
  http.get('http://sample123.com/file.jpeg', function(err, dlRes){
     if(err) return res.send(err.code) 
     if (dlRes.statusCode == 200 /* or all other possible ones*/)
        res.attachment(/* you could extract the filename from the path of http.get */)
        dlRes.pipe(res)
  })
})


this code is untested and just there to give you a clue. it uses nodes Streaming API.

Angelo Chen

unread,
Mar 13, 2013, 2:27:27 AM3/13/13
to Express
pipie works, thanks.

greelgorke

unread,
Mar 13, 2013, 5:01:19 AM3/13/13
to expre...@googlegroups.com
i have an eror in my example. here the better one:

app.get('some path', function(req,res,next){
  http.get('http://sample123.com/file.jpeg', function(dlRes){
     if (dlRes.statusCode == 200 /* or all other possible ones*/){
        res.attachment(/* you could extract the filename from the path of http.get */)
        dlRes.pipe(res)
     } else res.send(dlRes.statusCode)
  }).on('error', function(err){ res.send(err.code) })
})
Reply all
Reply to author
Forward
0 new messages