Reputation: 21
I have come accross the following html5 code which selects the .json from from local file system and displays its contents. But I need help in selecting the json file (without file upload dialog) from the pre-defined absolute path "C:\data.json"
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>On loaded file + AngularJS</title>
<script type='text/javascript' src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.12/angular.min.js"></script>
</head>
<body ng-app="myapp">
<div ng-controller="MainCtrl" class="container">
<h1>Select text file</h1>
<input type="file" on-read-file="showContent($fileContent)" />
<div ng-if="content">
<h2>File content is:</h2>
<pre>{{ content }}</pre>
</div>
</div>
<script type='text/javascript'>//<![CDATA[
var myapp = angular.module('myapp', []);
myapp.controller('MainCtrl', function ($scope) {
$scope.showContent = function($fileContent){
$scope.content = $fileContent;
};
});
myapp.directive('onReadFile', function ($parse) {
return {
restrict: 'A',
scope: false,
link: function(scope, element, attrs) {
var fn = $parse(attrs.onReadFile);
element.on('change', function(onChangeEvent) {
var reader = new FileReader();
reader.onload = function(onLoadEvent) {
scope.$apply(function() {
fn(scope, {$fileContent:onLoadEvent.target.result});
});
};
reader.readAsText((onChangeEvent.srcElement || onChangeEvent.target).files[0]);
});
}
};
});
//]]>
</script>
</body>
</html>
Upvotes: 0
Views: 394
Reputation: 378
What you're asking effectively is reading a local file without the client's explicit permission (having once being selected doesn't count), which has been avoided by design as it obviously introduces many kinds of threats. ;-)
It seems impossible to be done in any formal way. If you could explain on your actual goal, we might be able to suggest a different way.
Upvotes: 1