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


i have text in hidden textarea. when button clicked have text offered download .txt file. possible using angularjs or javascript?

you can using blob.

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

in controller:

var content = 'file content example'; var blob = new blob([ content ], { type : 'text/plain' }); $scope.url = (window.url || window.webkiturl).createobjecturl( blob ); 

in order enable url:

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

please note

each time call createobjecturl(), new object url created, if you've created 1 same object. each of these must released calling url.revokeobjecturl() when no longer need them. browsers release these automatically when document unloaded; however, optimal performance , memory usage, if there safe times when can explicitly unload them, should so.

source: mdn


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -