Read string generated by FileReader in result function

0

Next I'm getting a base 64 string but I can not get it to send to my server, it follows 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:

Please note that there is no string in the result that if I take it and play in an online converter my photo will be rendered. Now my big problem, why can not I assign this string to a variable or send that object to my server?

    
asked by anonymous 19.11.2017 / 03:47

1 answer

1

Apparently you are not searching for the string correctly.

I've had to do exactly the same job a while ago, however, I did it 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!

    
21.11.2017 / 20:48