Trying to download a pdf doc with Angular, we wrote this:
var _config = { headers : {'Accept' : '*/*'},
responseType : 'arraybuffer'
};
var success = function(data, status, header, config) {
$log.debug('Download resume success - type:' + typeof (data));
var _contentType = (header('Content-Type'));
var blob = new Blob([ data ], { type : _contentType });
var url = (window.URL || window.webkitURL).createObjectURL(blob);
var anchor = angular.element('<a/>');
anchor.attr({
href : url,
target : '_blank',
download : _fileName
})[0].click();
}
$http.get(_url, _config).success(success).error(error);
We've tried all permutations of blob and arraybuffer but data
always returns as a String with the extended characters 'decoded' which is to say broken.
_contentType
is always application/pdf
although we've tried forcing it to application/octet-stream
as well.
Suggestions and pointers welcomed!
Update
This looks to be a bug someplace between Angular (1.3.15 & 1.4.8) and Chrome's (46.0) XMLHttpRequest implementation where response.data is always returned as a 'decoded' string. Neither Firefox (42) nor IE (10) have this problem and all of the solutions below would most likely work (as does or original solution).
I've reported this as a possible bug to both AngularJS & Chrome.