Search code examples
javascriptangularjsnode.jspdffilesaver.js

How to save an incoming filestream from Node into AngularJS


On my express server I have a post-request which is getting a pdf file from Amazon S3 and sending it back to angular. This is how my express server endpoint ends with:

var fileStream = s3.getObject(options).createReadStream();
fileStream.pipe(res);

On the angular side of things I am posting and trying to save this

$http.post(url, data)
.then(function (data, status, headers, config) {
  var file = new Blob([data.data], {type: 'application/pdf'});
  FileSaver.saveAs(file, vm.projectName);
})

I get a pdf file which is not valid. I has about 300kb (more than original file) and I believe its not pdf but a buffer?

What I am trying to achieve: - Post Request to server - Server gets file from S3, Sends it back to Angular Controller - Angular Controller saves it as .png


Solution

  • When you're making POST call, set responseType of request to arrayBuffer inside configuration, so that response will get transformed to correctly.

    $http.post(url, data, {responseType: "arraybuffer"})