1 Reply Latest reply on Feb 17, 2016 8:14 PM by CalebEverett

    How to Send File for Download to Browser from /agreements/{agreementId}/combinedDocument

    CalebEverett

      I am trying to create a file from the body of the response, but it is creating a file that I can't open. It requires a password even though there isn't one on the file. I think this must have something to do with the encoding / decoding.

       

      I am using a node express server. Here are the few lines of code I am using:

       

      ```

          var request = require('request');

       

          request({

              baseUrl: 'https://api.na1.echosign.com/api/rest/v5',

              url: '/agreements/' + req.params.id + '/combinedDocument',

              headers: {'Access-Token': process.env.ECHOSIGN_INTEGRATIONKEY}

            },

            function(error, response, body){

              console.log(request.url);

              if(error) {

                res.send(error);

              }

              else {

                console.log(response.headers);

                var buf = new Buffer(body)

                res.set({

                  'Content-Disposition': 'attachment; filename=test.pdf',

                  'Content-Type': 'application/pdf; charset=utf-8'

                 });

                 res.write(buf);

                 res.end();

              }

            }

          );

      ```

        • 1. Re: How to Send File for Download to Browser from /agreements/{agreementId}/combinedDocument
          CalebEverett Level 1

          This is what ended up working in the end in case somebody else stumbles across this. I think the problem was that the data being returned from the API is a stream and it needed to have the chunking logic and then get concatenated in order to avoid getting corrupted.

           

          Also included is encoding to base64, pushing it into a database and then getting it back, decoding it and pushing it to the browser. I wasn't going to leave it like that, but had it set up that way to test the full cycle.

           

              router.get('/echosign/agreement/:id', function(req, res) {

                if (req.user !== 'myUserId') {

                  console.log(req.user);

                  res.redirect('/');

                } else {

             

                  var request = require('request');

                  var data = [];

             

                  request({

                    baseUrl: 'https://api.na1.echosign.com/api/rest/v5',

                    url: '/agreements/' + req.params.id + '/combinedDocument',

                    headers: {'Access-Token': process.env.ECHOSIGN_INTEGRATIONKEY}

                  }).on('data', function(chunk){

                      data.push(chunk);

                    })

                    .on('end', function(){

                      data = Buffer.concat(data).toString('base64');

             

                      client.connect(function(err) {

                        if(err) {

                          return console.error('could not connect to postgres', err);

                        }

                        client.query("UPDATE agreements SET file = '" + data + "' WHERE agreementid = '" + req.params.id + "' RETURNING agreement, file", function(err, result) {

                          if(err) {

                            return console.log(result, err);

                          }

                          client.end();

                          res.set({

                            'Content-Type': 'application/pdf;charset=UTF-8',

                            'Content-Disposition': "inline; filename='" + result.rows[0].agreement.name + ".pdf'"

                          });

                          res.send(new Buffer(result.rows[0].file, 'base64'));

                        });

                      });

                    });

                  }

              });