0

I'm trying grab the following PDF file from a node.js server running on Heroku. https://limitless-depths-2785.herokuapp.com/file/geo3a.pdf

If I go to that url within my browser, the PDF loads. What I would like to do is make an AJAX or GET call to that URL using jQuery or JavaScript and save it to the phone. I'm not sure exactly what I need to do on the server to configure it to send the pdf in a way that it can be saved to the phone or what call to make on the client side to receive that PDF file properly.

Here is my client side code.

$('#myButtonPdf').on('click', function () {
  alert("Loading Pdf");
  $.get( "https://limitless-depths-2785.herokuapp.com/file/geo3a.pdf", function( data ) {
       $( ".result" ).html( data );
       alert( "Loaded PDF" );
  });
});

Here is my Node.js server side code to serve up static files.

app.get('/file/:name', function (req, res, next) {

  var options = {
    root: __dirname + '/public/',
    dotfiles: 'deny',
    headers: {
        'x-timestamp': Date.now(),
        'x-sent': true
    }
  };

  var fileName = req.params.name;
  res.sendFile(fileName, options, function (err) {
    if (err) {
      console.log(err);
      res.status(err.status).end();
    }
    else {
      console.log('Sent:', fileName);
    }
  });

});
scottwalstead
  • 83
  • 1
  • 8

1 Answers1

0

You don't want to do an ajax request here. AJAX requests are for serialized data such as JSON, XML, HTML, etc. A PDF is transferred as binary data. Checkout out this post

Phonegap - Save image from url into device photo gallery

Just replace the image url with your PDF url

Further reading on the PhoneGap File plugin.

Community
  • 1
  • 1
Alex Hill
  • 713
  • 1
  • 5
  • 13