JrProgrammer
JrProgrammer

Reputation: 108

Data binding not available in other function

I'm trying to send a base64 encoded string to my server but the data binding I use is "" in the function to send it.

This is the code:

processFile: function(event) {
      var rawFile = event.target.files[0];
      var reader = new FileReader();
      reader.readAsDataURL(rawFile);
      reader.onload = function() {
        this.file = reader.result.split(',')[1];
      };
    },

So this.file contains the base64 string but when I access it in another function it's returning ""

What am I doing wrong here?

Upvotes: 0

Views: 48

Answers (1)

reinerBa
reinerBa

Reputation: 1660

Try

processFile: function(event) {
      var rawFile = event.target.files[0];
      var reader = new FileReader();
      reader.readAsDataURL(rawFile);
      reader.onload = function() {
        this.file = reader.result.split(',')[1];
      }.bind(this);
    },

or

processFile: function(event) {
      var rawFile = event.target.files[0];
      var reader = new FileReader();
      reader.readAsDataURL(rawFile);
      var vm = this;
      reader.onload = function() {
        vm.file = reader.result.split(',')[1];
      };
    },

Upvotes: 1

Related Questions