如何读取AngularJS中的文件? [英] How to read a file in AngularJS?

查看:246
本文介绍了如何读取AngularJS中的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以读取AngularJS中的文件?我想把文件放到一个HTML5画布上裁剪。



我在想使用指令?这是我要放入我的指令的javscript代码:

  function readURL(input){

if(input.files&& input.files [0]){
var reader = new FileReader();

reader.onload = function(e){
$('#blah')。attr('src',e.target.result);
}

reader.readAsDataURL(input.files [0]);
}
}


解决方案

是,指令是正确的方式,但它看起来有点不同:

  .directive(ngFileSelect,function(){
return {
link:function($ scope,el){
el.bind(change,function(e){
$ scope.file =(e.srcElement | | e.target).files [0];
$ scope.getFile();
});
}
}
})

工作示例: http://plnkr.co/edit/y5n16v?p=preview



感谢lalalalalmbda为此链接。 p>

Is it possible to read files in AngularJS? I want to place the file into an HTML5 canvas to crop.

I was thinking of using a directive? This is the javscript code I want to put into my directive:

function readURL(input) {

    if (input.files && input.files[0]) {
        var reader = new FileReader();

        reader.onload = function (e) {
            $('#blah').attr('src', e.target.result);
        }

        reader.readAsDataURL(input.files[0]);
    }
}

解决方案

Yes, directives is a right way, but it looks little bit different:

.directive("ngFileSelect",function(){    
  return {
    link: function($scope,el){          
      el.bind("change", function(e){          
        $scope.file = (e.srcElement || e.target).files[0];
        $scope.getFile();
      });          
    }        
  }
})

Working example: http://plnkr.co/edit/y5n16v?p=preview

Thanks to lalalalalmbda for this link.

这篇关于如何读取AngularJS中的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆