1

I am returning an array of bytes from a web api method that sets content-type as 'application/octet-stream. This array of bytes is for a pdf file. I am using angularjs.

Question : What will be the data value for href in code below for this case? I have set its as attachment/pdf which is not working.

 return reportsDataService.getMyData()
                    .then(function (data) {
                        var element = angular.element('<a/>');
                        element.attr({
                            href: 'data:attachment/pdf,' + url,
                            target: '_blank',
                            download: 'myreport.pdf'
                        })[0].click();
                    });

1 Answer 1

1

Per Blob documentation, you can code as below

var blob = new Blob([typedArray], {type: 'application/octet-binary'});
$scope.url= URL.createObjectURL(blob);

Then in your view

<a download="content.txt" ng-href="{{ url }}">download</a>

Last but not least, you should configure the compileProvider to enable the url

app.config(['$compileProvider',
function ($compileProvider) {
    $compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|ftp|mailto|tel|file|blob):/);
}]);

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.