3

I have a Javascript inside my .ts file and it keep showing me that property 'result' does not exist on type 'EventTarget' ???

imageUpload() {

window.onload = function(){   
    //Check File API support zzzz
        (<any>$('#files')).live("change", function(event) {
            var files = event.target.files; //FileList object
            var output = document.getElementById("result");
            for(var i = 0; i< files.length; i++)
            {
                var file = files[i];
                //Only pics
                // if(!file.type.match('image'))
                if(file.type.match('image.*')){
                    if(this.files[0].size < 2097152){    
                  // continue;
                    var picReader = new FileReader();
                    picReader.addEventListener("load",function(event){
                        var picFile = event.target;
                        var div = document.createElement("div");
                        div.innerHTML = "<img class='thumbnail' src='" + picFile.result + "'" +
                                "title='preview image'>";
                        output.insertBefore(div,null);            
                    });
                    //Read the image
                    $('#clear, #result').show();
                    picReader.readAsDataURL(file);
                    }else{
                        alert("Image Size is too big. Minimum size is 2MB.");
                        $(this).val("");
                    }
                }else{
                alert("You can only upload image file.");
                $(this).val("");
            }
            }                               

        });
}
}

In my HTML I used this input tag

    <input id="files" type="file" accept="image/*" multiple/>

And my output tag like this

Why there is error inside the javascript ?? Anyone know what is the problem ? THanks !!!

Reggie
  • 75
  • 1
  • 11

2 Answers2

5

Looks like the event.target is of type FileReader and therefore try casting the event.target to the particular type to tell TypeScript that it will, in fact, have a .result value:

var picFile = <FileReader>event.target;

See (here) for reference.

Forty3
  • 2,199
  • 1
  • 14
  • 19
0

changed

request.onsuccess = function (event) {
            resolve(event.target.result);
        }

to this

request.onsuccess = function (event) {
            var et = <FileReader>event.target;
            resolve(et.result);
        }
Steven
  • 118
  • 1