0

Ok so i am trying to do is to a save a file to the user's disk when they click on it. Here is code, ajax:

 $('.cv_download').on('click',function(){
        var fileName = $(this).text();
        console.log(fileName)
        var temp = {"file" : fileName}
        console.log(temp);
        console.log(JSON.stringify(temp))
        $.ajax({
            url: '/download-cv-file',
            type: 'POST',
            data: {fileSend:JSON.stringify(temp)},
            dataType: 'json'
        });
    })

in my server file i have:

app.post('/download-cv-file', function(req, res){ services.downloadFile(req,res); });

which fires :

var downloadFile = function(req,res){
console.log('From services')
console.log(req.body.fileSend)
var p_file = JSON.parse(req.body.fileSend);
console.log(p_file);
console.log('The file we look for is ' + p_file.file);
var file = 'services/cvs/'+p_file.file+'';
console.log('The file is ' + file);
res.download(file);}

Any ideas? Thank you!

Niko
  • 57
  • 4
  • it doesnt prompt me where do i want to save the file – Niko Dec 11 '14 at 14:26
  • there is no prompt in your code. does it write out anything to the console? – ABri Dec 11 '14 at 14:32
  • no , ican only see in the network that the request retruns 200 and finds the correct file with correct content-disposition and content-type. Maybe i am missing something ? I am still pretty new to this. – Niko Dec 11 '14 at 14:35
  • Maybe you can show an example for content deposition header? It should work. Which browsers where tested? – Max Dec 11 '14 at 15:03
  • I tested in Chrome and Opera this is my Response Header Accept-Ranges:bytes Cache-Control:public, max-age=0 Connection:keep-alive Content-Disposition:attachment; filename="CV.pdf" Content-Length:155105 Content-Type:application/pdf Date:Thu, 11 Dec 2014 15:16:02 GMT ETag:W/"DXHclQFA2V7cXEn3QNak4A==" Last-Modified:Thu, 11 Dec 2014 09:59:13 GMT X-Powered-By:Express – Niko Dec 11 '14 at 15:18

1 Answers1

0

Your problem is sending the file with ajax. It will not work. You can use the following workarounds: Create an iframe with the file url or use the file api of html5.

Here is a good reference for this: Download a file by jQuery.Ajax

Community
  • 1
  • 1
Max
  • 368
  • 3
  • 10