2

I've got a POST request like this:

 $scope.myFunction = function (name, id, value) {
      $http.post(/retrievePDFFiles, {
                    name: name,
                    id: id,
                    value: value
                }).success(function(data, headers) {
                    var filename = headers('filename');
                    if(data.byteLength > 250) {
                        var blob = new Blob([data], {type : 'application/pdf;charset=utf-8'});
                        saveAs(blob, filename);
                    } else {
                        console.log("Error");
                    }

                }).error(function(data) {
                   console.log(data);
                });
}

With this call i send some parameters to save them in a table in my db and as response i have a pdf stream. The request to the server it returns correctly 200 and all parameters are corrected and everything is saved in db but the pdf not works. I've got an error in console:

SyntaxError: Unexpected token %

and if i debug the request it goes in the .error function. I think the problem is that it doesn't recognize that it needs to download a stream and, i don't know, it doesn't work. I think just adding

responseType : 'arraybuffer'

somewhere it will works but i don't know where! I can't touch the structure of the parameters.. Any idea?

EDIT: i tried as written here with no results How to read binary data in AngularJS in an ArrayBuffer?

9
  • Possible duplicate of How to read binary data in AngularJS in an ArrayBuffer? Commented May 31, 2016 at 14:01
  • It's not a duplicate, i've already tried that way with no success Commented May 31, 2016 at 14:02
  • You passed the config properties as the third argument? get, and post use different number of arguments, get has the the config options as the second argument while post uses it as the third argument Commented May 31, 2016 at 14:03
  • 2
    No, in the post call $http.post(url,data,{responseType:"arraybuffer"}) Commented May 31, 2016 at 14:04
  • 1
    No, im saying add the config options to your call as a third argument, $http.post("/retrievePDFFiles",{name:name,id:id},{responseType:"arraybuffer"}) Commented May 31, 2016 at 14:07

1 Answer 1

0

Try this:

app.factory('apiService', ['$http', function($http){
    return {
        downloadFile: function(url, file) {
            return $http({
                url: url,
                method: 'POST',
                data: file,
                responseType: 'arrayBuffer'
            });
        }
    };
}]);

Controller

$scope.download = function(name, id, value) {
    //form the payload for file download
    var payload = {
        name: name,
        id: id,
        value: value
    };

    //execute service to download
    apiService.downloadFile('/retrievePdfFiles', payload).then(function(response) {
        //download the blob
        var contentType = response.headers()['content-type'] || octetStreamMime;
        var blob = new Blob([response.data], contentType);
    }).catch(function(response){
        //there's been an error
    });
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.