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

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

问题描述

是否有可能在AngularJS读取文件?我想将文件放入一个HTML5画布作物。我想用指令?

Hey guys, 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?

这是javscript code我希望把我的指令:

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();
      });          
    }        
  }

工作exapmle: http://plnkr.co/edit/y5n16v?p=$p$pview

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

感谢lalalalalmbda的<一个href=\"http://odeto$c$c.com/blogs/scott/archive/2013/07/03/building-a-filereader-service-for-angularjs-the-service.aspx\">this链接。

Thanks to lalalalalmbda for this link.

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

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