2
I am creating a web application and need to read a txt file that will be provided by the user. The code I’m using to perform the reading is not working and I can’t find anywhere else a way to do the reading.
// Código dentro do controller
$scope.imprimir = function () {
lerArquivoTxt.carregarArquivo();
}
// Código dentro do service
function _carregarArquivoTxt() {
var arquivoSelecionado = document.getElementById('arquivoPastas');
var divSaida = document.getElementById('textoLido');
var fileExtension = /text.*/;
var arquivoLeitura = arquivoSelecionado.files[0];
var fileReader = new FileReader();
fileReader.readAsText(arquivoLeitura);
divSaida.innerText = 'resultado: ' + fileReader.result;
}
<div class="col-sm-6 form-group" id="arquivo">
<div class="lbltitulo" for="Numero">Selecione o arquivo base para criar as pastas do projeto:</div>
<div class="tbpdd16">
<input type="file"
id="arquivoPastas"
class="hideBtn btn btn-warning">
</div>
<div class="">
<input type="text"
id="txtfiletoread"
class="form-control"
placeholder="Pasta / sub pasta"
ng-model="pasta.pasta">
</div>
<div id="textoLido"></div>
The idea is that the user selects a file through the input file, the controller calls the _load File() function of the service and this function read and return all the read file. I need help with the code that reads the txt file.
It has how to put all the code! mainly of Angular
– novic
You saw the example and then it served you?
– novic
I haven’t had time to test the proposed solution.
– Thiago Baldykowski
OK!!! Thiago ...
– novic
Virgilio Novic, the code you posted works if I create a new project and add the code reads the file and assigns the read text inside the div perfectly. However, applying this code to the project I’m creating doesn’t work. What I’d like to do is call this file read function through the service, assign the read content to a variable and then work with that variable and then display the read content in a table.
– Thiago Baldykowski
The problem of working in the service or any part of your code is a factor called in the world of computer adaptation, you have a functional example of code, you have not posted your complete angular code just excerpt, is either an answer to a solution that we do not know as has already been said, for the lack of code, or a code ready for its solution, post its angular code in full and point out where it wants it to work and how it works, ie edit your question and put it like it should be.
– novic
... Conti: it is also worth remembering that the
directive
created updates the$scope.text
which is an object with the fieldvalue
that can and should be used in the best possible way. It was done so by the logic of selected the file.txt
and right away show the result, but, reaffirming missing data in your question and editing would be the best way to point out what the answer would be.– novic
I was able to make the code work, the problem was that I was assigning the Reader.result to an array, and not to an object like you showed in the code.
– Thiago Baldykowski
Okay that’s good when you can work it out, if the answer suits you
– novic