2

I'm trying to get AngularJS to trigger a file download when a user clicks on a button.

The file that should download has to contain data that is generated during the script execution, so the text file's content should be built from a basic string in AngularJS.

How do I implement this?

Here is some mockup code on how I imagine this would work:

var fileContent = 'foobar';
$scope.download = function() {
  filedownload.run(fileContent, 'filename.txt');
}
2

2 Answers 2

4

In order to achieve this you have to create an a tag in your HTML:

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

Controller:

var content = 'file content for example';
var blob = new Blob([ content ], { type : 'text/plain' });
$scope.url = (window.URL || window.webkitURL).createObjectURL( blob );

And to enable the URL:

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

Source: How do you serve a file for download with AngularJS or Javascript?

Sign up to request clarification or add additional context in comments.

1 Comment

Use aHrefSanitizationWhitelist = /^\s*(https?|ftp|mailto|tel|file|blob):/. For more information see AngularJS sanitizeUri.js source code line #9.
2

In simple cases, you can create a link with a Data URI:

var fileContent = 'foobar';
$scope.download = 'data:text/plain;base64,' + btoa(fileContent);

And then use it in your HTML template:

<a href="{{download}}">click here to download</a>

You may need to adapt the MIME type, depending on your string.

4 Comments

Thanks. This is an interesting approach. While this does generate a valid link, the download gets rejected in chrome due to an unsafe link error. For explanation: I want to create a vCard file in Angular to download and this is not the right solution. Do you have a follow up idea? Thanks!
Maybe Chrome rejects it only when the site is localhost? Other than that, I am out of ideas, sorry.
I actually found a solution myself now. It's basically the same idea as yours

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.