MicroMan
MicroMan

Reputation: 2098

translate string to downloadable txt file Angularjs

I have a table which is generated using a angular repeat

<tr ng-repeat="result in vm.results">
    <td>
        <a download="Generated.txt" href="data:text/plain;charset=utf-8," + {{result.generatedOutput}}>Download</a>
    </td>
<tr>

I have checked the results and result.generatedOutput has a value in it. But when ever i download the file it is blank. If i hardcode the value in like follows it works

<a download="Generated.txt" href="data:text/plain;charset=utf-8,sosososososlaksakldsa">Download</a>

Upvotes: 0

Views: 912

Answers (2)

lucasnadalutti
lucasnadalutti

Reputation: 5958

Use ng-href instead of href. This will force the href attribute to only be constructed when Angular has parsing the file, which includes the {{}} values. More about ng-href here.

<a download="Generated.txt" ng-href="data:text/plain;charset=utf-8,{{result.generatedOutput}}">Download</a>

Upvotes: 0

W4pp
W4pp

Reputation: 145

The string should be added to href as follows:

<tr ng-repeat="result in vm.results">
    <td>
        <a download="Generated.txt" href="data:text/plain;charset=utf-8,{{result.generatedOutput}}">Download</a>
    </td>
<tr>

Also note that in Chrome this approach doesn't work, read more here: Force download of 'data:text/plain' URL

Upvotes: 1

Related Questions