How to read the first line of .txt file?

espresso_coffee

can anyone one help how to read the first line of .txt file? I have input type file that upload example.txt then I would like to grab the first line from that code. I tried something like this:

<input type="file" id="fileUpload" name="fileUpload"/> MyTest.txt //file name

function confirmFileSubmit(){
    var fileName = $('#fileUpload').val();
    alert($('#fileUpload').split('\n')[0]);
} 

After I run my code this just outputted file name in alert box. I'm not sure how I can read the content of the file. If anyone can help please let me know.

adeneo

You'll need the FileReader for that

function confirmFileSubmit(){
    var input  = document.getElementById('fileUpload'); // get the input
    var file   = input.files[0];                  // assuming single file, no multiple
    var reader = new FileReader();

    reader.onload = function(e) {
        var text = reader.result;                 // the entire file

        var firstLine = text.split('\n').shift(); // first line 

        console.log(firstLine);                   // use the console for debugging
    }

    reader.readAsText(file, 'UTF-8');             // or whatever encoding you're using
                                                  // UTF-8 is default, so this argument 
}                                                 // is not really needed

Este artigo é coletado da Internet.

Se houver alguma infração, entre em [email protected] Delete.

editar em
0

deixe-me dizer algumas palavras

0comentários
loginDepois de participar da revisão

Artigos relacionados

How to read a specified line in a txt file (batch)

How to read only the second line of a .txt file?

How to read first line of a file twice?

How to read a special line in .txt file using Fortran?

Read a txt file every first, 2nd and 3rd line and save in 3 different lists

Read a txt file with \n in between the line

How can I read in a TXT file in Access that is over 255 char/line and contains control char?

How to read binary file and write to .txt file

Python] How to modify the value from the first 'read' txt to 'write' txt

How to read text file line by line?

How to read a file line by line in PHP

how to read text file line to line in python

How to read the first line from a file as key and the next 3 lines as a list of values to a dictionary, python

How to print first N bytes of a txt file?

Read txt file to pandas dataframe with unique delimiter and end of line

How to read .txt file from assets in Flutter?

how to read the content of .txt file using python?

How to delete a line from a txt file in Qt?

How to read file in line and back to specific line to read it again

In C, how to print out a txt file line by line?

How to Read and Return Random Line From Txt(C)

How to read first 1000 entries in a csv file

How to read only first part of a file in Swift?

How to skip reading the first line of file?

Read a "TXT" file in Dart

Laravel 5.6 how to read text file line by line

How to read file line by line and print to a text box c#

How to store first N strings from a txt file in Python?

How to read different .txt file columns in different strings in Python?

TOP lista

  1. 1

    R Shiny: use HTML em funções (como textInput, checkboxGroupInput)

  2. 2

    O Chromium e o Firefox exibem as cores de maneira diferente e não sei qual deles está fazendo certo

  3. 3

    Como assinar digitalmente um documento PDF com assinatura e texto visíveis usando Java

  4. 4

    R Folheto. Dados de pontos de grupo em células para resumir muitos pontos de dados

  5. 5

    Gerenciar recurso shake de Windows Aero com barra de título personalizado

  6. 6

    Como obter dados API adequados para o aplicativo angular?

  7. 7

    UITextView não está exibindo texto longo

  8. 8

    Por que meus intervalos de confiança de 95% da minha regressão multivariada estão sendo plotados como uma linha de loess?

  9. 9

    Acessando relatório de campanhas na AdMob usando a API do Adsense

  10. 10

    Usando o plug-in Platform.js do Google

  11. 11

    Como posso modificar esse algoritmo de linha de visada para aceitar raios que passam pelos cantos?

  12. 12

    Dependência circular de diálogo personalizado

  13. 13

    Coloque uma caixa de texto HTML em uma imagem em uma posição fixa para site para desktop e celular

  14. 14

    iOS: como adicionar sombra projetada e sombra de traço no UIView?

  15. 15

    Como usar a caixa de diálogo de seleção de nomes com VBA para enviar e-mail para mais de um destinatário?

  16. 16

    Tabela CSS: barra de rolagem para a primeira coluna e largura automática para a coluna restante

  17. 17

    How to create dynamic navigation menu select from database using Codeigniter?

  18. 18

    Converter valores de linha SQL em colunas

  19. 19

    ChartJS, várias linhas no rótulo do gráfico de barras

  20. 20

    用@StyleableRes注释的getStyledAttributes。禁止警告

  21. 21

    não é possível adicionar dependência para com.google.android.gms.tasks.OnSuccessListener

quentelabel

Arquivo