如何使用angularjs通过点击按钮下载图像到本地
How to download an image by clicking on button into our local using angularjs?
嗨,我是angularjs
的新手,我在stackoverflow上看到了很多关于这个的问题,但是没有找到一个好的解决方案。
<button ng-click="download()">download</button>
我的要求是(1)我不想使用<a>
标签
(2)我不想使用<download>
属性,因为它应该在所有浏览器中支持。
当用户点击下载按钮时,图像应该被下载到客户端的本地机器。
假设图像来自某个url
<script>
angular.module('myApp', []);
angular.module('myApp').controller('HomeCtrl', ['$scope', '$http', function($scope, $http) {
$scope.download=function()
{
$http.get(url).success(function(data){
// code to download image here
}).error(function(err, status){})
}
}]);
</script>
angular.module('myApp', []).controller('HomeCtrl', ['$scope', '$http',
function($scope, $http) {
$scope.download = function() {
$http.get('https://unsplash.it/200/300', {
responseType: "arraybuffer"
})
.success(function(data) {
var anchor = angular.element('<a/>');
var blob = new Blob([data]);
anchor.attr({
href: window.URL.createObjectURL(blob),
target: '_blank',
download: 'fileName.png'
})[0].click();
})
}
}
]);
<!doctype html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
</head>
<body ng-app="myApp">
<div ng-controller="HomeCtrl">
<button ng-click="download()">download</button>
</div>
</body>
</html>
您可以借助BLOB对象
来实现这一点HTML<body ng-app="myApp">
<div ng-controller="HomeCtrl">
<button ng-click="download()">download</button>
<img id="photo"/>
</div>
</body>
角代码:
angular.module('myApp', []);
angular.module('myApp').controller('HomeCtrl', ['$scope', '$http', function($scope, $http) {
$scope.download=function()
{
$http.get('https://placeholdit.imgix.net/~text?txtsize=15&txt=image1&w=120&h=120', {responseType: "arraybuffer"}).success(function(data){
var arrayBufferView = new Uint8Array( data );
var blob = new Blob( [ arrayBufferView ], { type: "image/png" } );
var urlCreator = window.URL || window.webkitURL;
var imageUrl = urlCreator.createObjectURL( blob );
var img = document.querySelector( "#photo" );
img.src = imageUrl;
// code to download image here
}).error(function(err, status){})
}
function SaveToDisk(fileURL, fileName) {
// for non-IE
if (!window.ActiveXObject) {
var save = document.createElement('a');
save.href = fileURL;
save.target = '_blank';
save.download = fileName || 'unknown';
var event = document.createEvent('Event');
event.initEvent('click', true, true);
save.dispatchEvent(event);
(window.URL || window.webkitURL).revokeObjectURL(save.href);
}
// for IE
else if ( !! window.ActiveXObject && document.execCommand) {
var _window = window.open(fileURL, '_blank');
_window.document.close();
_window.document.execCommand('SaveAs', true, fileName || fileURL)
_window.close();
}
}
}]);
普朗克的解决方案:http://plnkr.co/edit/IKQKWkY6YMwodzpByx0f?p =预览新普朗克自动下载:http://plnkr.co/edit/eevPO2fh3F37Yhvchnol?p=preview
相关文章:
- 如何在Edge中下载图像/png数据URI
- 将SVG下载为PNG图像
- 将图像下载为数据url时出错
- node.js从http请求列表中将图像下载到服务器
- 在阵列中预加载图像(并等待图像下载)
- 如何以编程方式将图像下载到iPad
- 异步:1. 发布网址和 2.图像下载(从给定的 URL)
- 强制图像下载 - 缺少文件扩展名
- 创建图像下载按钮支持所有浏览器
- 强制将远程托管的图像下载到浏览器
- 使用Google Analytics跟踪图像下载
- 单击图像下载链接
- Wordpress的图像下载按钮
- 在页面上的其他几个图像下载完成后,HTML视频开始下载
- 如何获得图像下载时,用户点击
- 更改图像的src属性是否会阻止图像下载?
- 使用javascript为用户提供图像下载
- 保护图像下载理论
- 如何将画布图像下载位置更改为用户特定位置
- 如何在HTML中为移动页面创建图像下载链接?