1

I'm trying to implement FileSaver.js like this example https://github.com/alferov/angular-file-saver to download generated file by spring boot application ,I have rest service that return the file as byte array data.

@RequestMapping(value = "/generateReport/{reportId}/{parameters}",
        method = RequestMethod.GET,
        produces = MediaType.APPLICATION_JSON_VALUE)
    @Timed
    public  byte[] generateReport(@PathVariable("reportId") String reportId,
                                @PathVariable("parameters") String parameters
                                ) {

        byte[] bFile;
        //some code

        return bFile;
     }

Angular service

'generateReport': {
            method: 'GET',
            responseType: 'arraybuffer' ,
            url: 'adap_report/api/generateReport/:reportId/:parameters',
            transformResponse: function (data) {
                if (data) {
                    data = angular.fromJson(data);
                }
                return data;
                }
        },

Angularjs controller

 vm.generateReport = function() {
        Report.generateReport({reportId:entity.id,parameters:angular.toJson(vm.parameterList)}, function(result) {
        var data = new Blob([result], { type: 'application/octet-stream' });
        FileSaver.saveAs(data, 'text.txt');
        });
    };

File saver works and file downloaded but The content is wrong ,I just get file with this content

[object Object]  

Can anyone please help me to save generated file with angular FileSaver.js library?

1 Answer 1

2

I have solved it by http

 $http.get('/adap_report/api/generateReport/'+entity.id+'/'+angular.toJson(vm.parameterList), {responseType: 'arraybuffer'})
          .success(function (data) {
              console.log(data)

              if(entity.reportoutputtypecode=="PDF"){
                  var blobData = new Blob([data], {type: 'application/pdf'});
                  var fileURL = URL.createObjectURL(blobData);
                  window.open(fileURL);               
              }else{
                var blobData = new Blob([data], {type: 'application/'+entity.reportoutputtypecode});
                FileSaver.saveAs(blobData, 'jasper-file.'+entity.reportoutputtypecode);
             }
          }); 
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.