Dependencies
Installation
Using bower: bower install angular-file-saver
Using npm: npm install angular-file-saver
Basic usage
- Include
ngFileSaver module into your project;
- Pass both
FileSaver and Blob services as dependencies;
- Create a Blob object by
passing an array with data as the first argument and an object with set of options
as the second one:
new Blob(['text'], { type: 'text/plain;charset=utf-8' });
- Invoke
FileSaver.saveAs with the following arguments:
data Blob: a Blob instance;
filename String: a custom filename (an extension is optional);
disableAutoBOM Boolean: (optional) Disable automatically provided Unicode text encoding hints.
You can do so by injecting FileSaver and Blob into the controller and then using the syntax as shown below:
angular.module('sample',['ngFileSaver'])
.controller('ConsultationDetailsController', ['$scope', 'FileSaver', 'Blob', function($scope, FileSaver, Blob){
$scope.download = function (fileName) {
$scope.isLoading = true;
downloadHttpService.getDocument(fileName)
.then(function (response) {
var data = response.data;
var status = response.status;
var header = response.headers();
var fileType = header['content-type'];
var blob = new Blob([data], { type: fileType });
FileSaver.saveAs(blob, originalFileName);
})
.catch(function (resp) {
// show error
})
.finally(function (data) {
// execute finally block.
});
};
}]);
if you want only the pdf type then you can hard coded define
fileType as 'application/pdf' like this var fileType= 'application/pdf';
Hope this solves your problem :)
[object Object]or this format using JSON.stringify(){"0":"%","1":"P","2":"D","3":"F","4":"-","5":"1","6":".","7":"7","8":"\n",. I am unable to get the proper render able pdf