0

My aim is to download files on browser window with res.download() and I am following this post.But file is not getting downloaded in the browser.In my console(grunt) it returns 200 response but noting happens on the front end. Controller:

$http.get('/api/knowledges/file_download/?filepath='+fileDownload.file_details.path).success(function(data){
            console.log("and the api returned success")
        });

Api:

res.download(req.query.filepath, 'my.pdf');

And this is the JSON which I am giving to the server:

fileDownload=  {
    destination:"server/uploads/docs"
    encoding:"7bit"
    fieldname:"file"
    filename:"9b129f186ca7c534dbfb90e6c2762226"
    mimetype:"application/pdf"
    originalname:"Dress Code.pdf"
    path:"server\uploads\docs\9b129f186ca7c534dbfb90e6c2762226"
    size:397860}
Community
  • 1
  • 1
Akshay
  • 25
  • 7
  • You can't trigger downloads from XHR requests, here's a decent solution: http://stackoverflow.com/a/43523297 – robertklep May 09 '17 at 12:33
  • Nah,I am using node and angular to accomplish the task.Javascript will not be handy enough to solve it. – Akshay May 10 '17 at 04:01
  • @robertklep how is the question the EXACT duplicate of mine? – Akshay May 11 '17 at 03:59
  • I cant see any api code in that question.My query is why my res.download() ain't workin – Akshay May 11 '17 at 04:00
  • Your issue isn't `res.download()` not working (because it's working just fine; like you wrote yourself, _"it returns a 200 response"_), the issue is that you can't trigger a browser download from an XHR response. The question I linked to provides a solution for that. The other link I provided, in my comment, also provides a solution. – robertklep May 11 '17 at 06:30
  • @robertklep it is giving a 200 response online when I am downloading the file for the very first time.And it is throwing an error after that which is: – Akshay May 12 '17 at 11:24
  • server\uploads\docs\50dbe0e232e190fd377439293a0dc36b GET /api/knowledges/file_download/?filepath=server\uploads\docs\50dbe0e232e190fd 377439293a0dc36b 304 422ms error thrown { Error: Request aborted at Immediate. (C:\project\node_modules\express\lib\response.js: 941:17) at runCallback (timers.js:637:20) at tryOnImmediate (timers.js:610:5) at processImmediate [as _immediateCallback] (timers.js:582:5) code: 'ECONNAB ORT' } – Akshay May 12 '17 at 11:25
  • Are you passing a callback function to `res.download()`? And which version of Express are you using? – robertklep May 12 '17 at 11:32
  • @robertklep yes I am using callback to console log the error and my express details are {"express": "~4.9.0", "express-jwt": "^3.0.0", "express-session": "~1.0.2"} – Akshay May 15 '17 at 04:02
  • Did you implement the answer in the linked question to see if that fixes the download? – robertklep May 15 '17 at 06:49
  • yes.It dint help – Akshay May 15 '17 at 08:54
  • If you add the specific issues that you are seeing to your question, I'll vote to reopen it (although I think that the `ECONNABORT` is a red herring, and that there's nothing wrong with `res.download()`). – robertklep May 15 '17 at 09:16
  • I have tried using file stream and below is the code...Now I am getting 200 response for each and every click but still not getting any download in the browser : – Akshay May 15 '17 at 09:33
  • var file=req.query.filepath; var filename = path.basename(file); var mimetype = mime.lookup(file); res.setHeader('Content-disposition', 'attachment; filename=' + filename); res.setHeader('Content-type', mimetype); var filestream = fs.createReadStream(file); filestream.pipe(res); – Akshay May 15 '17 at 09:33
  • As long as you keep requesting the URL using XHR (with `$http.get()`), you will never get a download dialog. The link in my very first comment (http://stackoverflow.com/a/43523297/893780) provides a solution, and a similar solution is also provided for the duplicate question (but read that question and its answer _carefully_; it will use `$http.get()` but only to retrieve the URL to download; you already have that URL, so the only thing you have to implement is the part after _"//=== add tag and trigger click ==="_) – robertklep May 15 '17 at 09:37
  • Nah man.My file is on server...I cant access that just by giving the route.Please understand the question before marking it as a duplicate. – Akshay May 15 '17 at 11:53
  • You know the URL of the file on the client, don't you? `/api/knowledges/file_download/?filepath='+fileDownload.file_details.path`. There's nothing stopping you from plugging that URL into the code that's being shown. – robertklep May 15 '17 at 11:57
  • You mean something like this? – Akshay May 15 '17 at 12:14
  • $scope.download=function(fileDownload){ function downloadFile(filePath){ console.log("download funcion invoked"); console.log(filePath); var link=document.createElement('a'); link.href = filePath; link.download = filePath.substr(filePath.lastIndexOf('/') + 1); link.click(); } downloadFile('/api/knowledges/file_download/?filepath='+fileDownload.file_‌​details.path); } – Akshay May 15 '17 at 12:14
  • Use `link.download = "my.pdf"` instead of `filePath.substr(...)`. – robertklep May 15 '17 at 12:19
  • I got this error:Running "concurrent:server" (concurrent) task Warning: Running "babel:server" (babel) task Warning: client/app/getNotes/getNotes.controller.js: Unexpected character '?' (2 1:75) Use --force to continue. Aborted due to warnings. – Akshay May 15 '17 at 12:21
  • The code you pasted contains unprintable characters, so you need to clean it up first. – robertklep May 15 '17 at 12:22
  • Running "concurrent:server" (concurrent) task Warning: Running "babel:server" (babel) task Warning: client/app/getNotes/getNotes.controller.js: Unexpected character '?' (21:75) Use --force to continue. Aborted due to warnings. – Akshay May 15 '17 at 12:25
  • Hello,Now file is getting downloaded in my browser but the content is not being loaded.Its like its a blank pdf. – Akshay May 16 '17 at 10:59

0 Answers0