Open CSV file and read its contents

Asked

Viewed 3,378 times

2

I have the information from the CSV file on console.log back-end, I would need to access this file and read its contents, some hint?

This is the code snippet from the back end that receives the file:

exports.anexo = function(req, res){

  var arquivo = req.files.uploads;
  var date = new Date();
  var dateTime = date.getTime();
  console.log(arquivo);

  var fileList = [];
    if(arquivo != undefined){
        for(var i=0; i < arquivo.length; i++){
            if(arquivo[i].originalFilename != undefined){
                var url_arquivo = dateTime + '_' + arquivo[i].originalFilename;
                var tipo_arquivo = arquivo[i].type;
                var path_origem = arquivo[i].path;
                var path_destino = './uploads/' + url_arquivo;

                var File = {
                    url_arquivo: url_arquivo
                };

                fileList.push(File);
            }
        }
    }
}

And here’s the part of the code on the front end that reads the file and sends it back-end

anexaArquivos(fileList : File[]) : Promise<any> {
    console.log('chamou a função no regiao.service')
    return new Promise((resolve, reject ) => {
      let formData : FormData = new FormData()
      let xhr : XMLHttpRequest = new XMLHttpRequest()

      if(fileList.length > 0){
        for( let i = 0; i < fileList.length; i++){
          formData.append("uploads[]", fileList[i], fileList[i].name);
        }
      }
      xhr.onreadystatechange = function () {
          if (xhr.readyState === 4) {
              if (xhr.status === 200) {
                  resolve(JSON.parse(xhr.response))
              } else {
                  reject(xhr.response)
              }
          }
      }

      xhr.open('POST', `${SES_API}/regiao/importar`, true);
      xhr.setRequestHeader('Authorization', `Bearer ${this.userService.sessao.accessToken}`);
      xhr.send(formData);
    })
  }

1 answer

0

There is a stop called D3.js that serves to help you use data, you can use it by adding:

<script src="https://d3js.org/d3.v4.min.js"></script>

(here is the github link with information about D3 https://github.com/d3/d3/wiki)

That’s a pretty easy way to put the line src above after you just use:

d3.csv(arquivo,funcao)

Where file is the name of the file with end . csv and function is a function you will create where you will run what you want.

  • You have placed a link to a PDF book, in addition to the link being invalid, it is not allowed to distribute material in this way on the site, unless the material is freely distributed. If the book is not available free by the author or publisher, it is piracy and is not allowed on the site.

  • I solved with Filereader and Readastext, but I will search on D3, thanks!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.