Jeepers Jeepers - 1 month ago 21
Javascript Question

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>

Answer

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