I have the CSV file information in the console.log
of the backend, I would need to access this file and read its contents, any tips?
This is the back-end code snippet 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 code part 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);
})
}