我有一些文本在一个隐藏的文本区域。当点击按钮时,我想要提供下载的文本作为.txt文件。这是可能使用AngularJS或Javascript?
你可以使用Blob做这样的事情。
原文链接:https://www.f2er.com/angularjs/146891.html<a download="content.txt" ng-href="{{ url }}">download</a>
在您的控制器:
var content = 'file content for example'; var blob = new Blob([ content ],{ type : 'text/plain' }); $scope.url = (window.URL || window.webkitURL).createObjectURL( blob );
以启用网址:
app = angular.module(...); app.config(['$compileProvider',function ($compileProvider) { $compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|ftp|mailto|tel|file|blob):/); }]);
请注意
Each time you call createObjectURL(),a new object URL is created,even if you’ve already created one for the same object. Each of these must be released by calling URL.revokeObjectURL() when you no longer need them. Browsers will release these automatically when the document is unloaded; however,for optimal performance and memory usage,if there are safe times when you can explicitly unload them,you should do so.
资料来源:MDN