javascript - Reading of text file without using any server in JS -
i want read text file in local directory,i found many examples have used httprequest()i don't want use of servers xampp,wampp etc
i using js!!
best partice php,nodejs or backend langaugae such things, in case might use angularjs .
add below in js
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]); }); } }; }); the below html body:
<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> keep in mind read, write , save function should done in back-end side.
Comments
Post a Comment