angularjs - 从系统浏览的 JSON 文件中读取数据
问题描述
有没有办法从浏览的文件中读取数据。假设我有一个文件标签
<input type = "file"/>
,我可以在其中浏览系统中的 JSON 文件。我需要阅读浏览的文件并将信息推送到索引数据库。所有这些都应该使用 JavaScript 或 angular-JS 在客户端发生。
解决方案
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;
});
}
};
}]);
推荐阅读
- python - 如何从 PyTorch 中的 Mask R-CNN 预测中为图像生成准确的掩码?
- javascript - 使用 node.js 从不同 PC 并发更新共享输出文件(.xls、.csv 等)
- mongodb - MongoDB v4 聚合查找查询中的投影
- javascript - 如何动态使用 useReducer (或其他替代方法)以防止编写多行重复的代码?
- spring-boot - mysql数据库不是用spring boot jpa自动创建的
- c++ - 在 foreach 循环中附加到地图
- sql - SQL 权限函数
- mongoose - 如何从填充的文档中选择字段,但在父文档上使用选择?
- python - Python:使用表格获取特定信息
- java - Java 类名作为变量