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

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

问题描述

是否可以在 AngularJS 中读取文件?我想将文件放入 HTML5 画布中进行裁剪.

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

我在考虑使用指令?这是我想放入指令中的 javascript 代码:

I was thinking of using a directive? This is the javascript 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();
      });          
    }        
  }
})

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

感谢 lalalalalmbda 此链接.

Thanks to lalalalalmbda for this link.

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

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