首页 > 解决方案 > 从系统浏览的 JSON 文件中读取数据

问题描述

有没有办法从浏览的文件中读取数据。假设我有一个文件标签
<input type = "file"/>,我可以在其中浏览系统中的 JSON 文件。我需要阅读浏览的文件并将信息推送到索引数据库。所有这些都应该使用 JavaScript 或 angular-JS 在客户端发生。

标签: angularjsfile-uploadindexeddb

解决方案


With this code i was able to sort out the issue.

  angular.module('app').directive('fileModel', ['$parse', function ($parse) {
        return {
            restrict: 'A',
            link: function(scope, element, attrs) {
                var model = $parse(attrs.fileModel);
                var modelSetter = model.assign;

                element.bind('change', function(e){
                   scope.myFile = element[0].files[0];
                   var reader = new FileReader();
                   reader.readAsText(scope.myFile);
                   var dataFromFile = reader.result;

                });
            }
        };  
    }]);

推荐阅读