Reputation: 1698
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?
Upvotes: 1
Views: 1070
Reputation: 1698
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);
}
});
Upvotes: 2