加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-17 09:08:47 所属栏目:安全 来源:网络整理
导读:我有一些文本在一个隐藏的文本区域。当点击按钮时,我想要提供下载的文本作为.txt文件。这是可能使用AngularJS或Javascript? 你可以使用Blob做这样的事情。 a download="content.txt" ng-href="{{ url }}"download/a 在您的控制器: var content = 'file co
我有一些文本在一个隐藏的文本区域。当点击按钮时,我想要提供下载的文本作为.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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读