UpA*_*ght 6 html javascript filereader angularjs
我正在尝试将一个csv文件加载到AngularJS中,以便我可以对内容进行一些操作.它不像我想要的那样工作.为了测试它是否正确加载,我将它加载到textarea中以查看内容.
当我加载文件时,它说它已正确加载但是onload()事件似乎没有触发,直到我加载第二个CSV文件,在这种情况下FIRST文件显示在textarea中.
HTML:
<div>
<input ng-model="csv"
onchange="angular.element(this).scope().fileChanged()"
type="file" accept=".csv" id="fileInput"/>
</div>
<br/>
<div>
<textarea ng-model="csvFile" readonly="true" style="width:99%; height:500px" disabled></textarea>
</div>
Run Code Online (Sandbox Code Playgroud)
JS:
$scope.fileChanged = function() {
$scope.$apply(function() {
var csvFileInput = document.getElementById('fileInput');
var reader = new FileReader();
var csvFile = csvFileInput.files[0];
reader.onload = function(e) {
$scope.csvFile = reader.result;
};
reader.readAsText(csvFile);
});
};
Run Code Online (Sandbox Code Playgroud)
当我把它放到JSFiddle中时,文件根本不会出现在textarea中.我是JSFiddle的新手,所以我不知道为什么会这样.
任何帮助都将不胜感激.
prz*_*zno 15
重新排序代码的某些行,希望评论足够解释
$scope.fileChanged = function() {
// define reader
var reader = new FileReader();
// A handler for the load event (just defining it, not executing it right now)
reader.onload = function(e) {
$scope.$apply(function() {
$scope.csvFile = reader.result;
});
};
// get <input> element and the selected file
var csvFileInput = document.getElementById('fileInput');
var csvFile = csvFileInput.files[0];
// use reader to read the selected file
// when read operation is successfully finished the load event is triggered
// and handled by our reader.onload function
reader.readAsText(csvFile);
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16138 次 |
| 最近记录: |