Read Filereader generated string in result function

Asked

Viewed 360 times

0

Next I’m able to generate a string in base 64 but I can’t send it to my server, follow the code:

      $scope.cadastraFoto = function(){
    var input = document.getElementById('fotoPerfil').files[0]

    var reader = new FileReader()
    reader.readAsDataURL(input)
    console.log(reader)
    var teste = reader.result
    console.log(teste)

  }

The result on the console looks like this : inserir a descrição da imagem aqui

Note that in the result there is a string that if I pick it up and play in an online converter my photo will be rendered. Now my big problem, because I can’t assign this string to a variable or send this object to my server ?

1 answer

1

Apparently you are not searching the string the right way.

I already had to do exactly the same job a while ago, however, I did in jquery:

var fileReader = new FileReader();
var anexo = $("#seuInput").get(0).files[0];
var nomeAnexo = anexo.name;
var dataUrl = "";

fileReader.filename = nomeAnexo;
fileReader.onloadend = function(result) {

    //Atribui string em base 64 à variável
    dataUrl = fileReader.result.split(';base64,')[1];

    //-- Chame aqui sua função para enviar os dados para o servidor

};
fileReader.readAsDataURL(anexo);

I hope it helps you. Good luck!

Browser other questions tagged

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