如何使用AngularJS或Javascript提供下载的文件?

前端之家收集整理的这篇文章主要介绍了如何使用AngularJS或Javascript提供下载的文件?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一些文本在一个隐藏的文本区域。当点击按钮时,我想要提供下载的文本作为.txt文件。这是可能使用AngularJS或Javascript?
你可以使用Blob做这样的事情。
<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

原文链接:https://www.f2er.com/angularjs/146891.html

猜你在找的Angularjs相关文章