最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Cannot read property 'target' of undefinedwhen trying to access <input> files - Stack

programmeradmin3浏览0评论

I'm trying to change file upload image to select or click on image on the same page in this canvas related script.

function aFileIsLoaded(e1)
{
        var filename = e1.target.files[0];
        var fr = new FileReader();
fr.onload = function(e2)
{
      backgroundimage = new Image();
      backgroundimage.src=e2.target.result;
     var context = document.getElementById('myCanvas').getContext('2d');
     context.canvas.width = backgroundimage.width;
     context.canvas.height = backgroundimage.height;
     context.drawImage(backgroundimage, 0, 0, backgroundimage.width, backgroundimage.height);


};
    fr.readAsDataURL(filename);
}



window.onload=function(){
var s = document.getElementById("fontsize");
s.value="48";
document.getElementById('loadpicture').addEventListener('change', aFileIsLoaded, false);
backgroundimagemode=NONE;
carpeInit();
update();
}

before, this images were called like this, and files were selected from desktop

<input type="file" name="back" id="loadpicture" src="myimage.png" >

and now I'm trying to load an image in the same page with this:

<img src="myimage.png" name="back" id="loadpicture" onclick="aFileIsLoaded()">

but I'm getting this error:

Uncaught TypeError: Cannot read property 'target' of undefined 

any help will be appreciated

I'm trying to change file upload image to select or click on image on the same page in this canvas related script.

function aFileIsLoaded(e1)
{
        var filename = e1.target.files[0];
        var fr = new FileReader();
fr.onload = function(e2)
{
      backgroundimage = new Image();
      backgroundimage.src=e2.target.result;
     var context = document.getElementById('myCanvas').getContext('2d');
     context.canvas.width = backgroundimage.width;
     context.canvas.height = backgroundimage.height;
     context.drawImage(backgroundimage, 0, 0, backgroundimage.width, backgroundimage.height);


};
    fr.readAsDataURL(filename);
}



window.onload=function(){
var s = document.getElementById("fontsize");
s.value="48";
document.getElementById('loadpicture').addEventListener('change', aFileIsLoaded, false);
backgroundimagemode=NONE;
carpeInit();
update();
}

before, this images were called like this, and files were selected from desktop

<input type="file" name="back" id="loadpicture" src="myimage.png" >

and now I'm trying to load an image in the same page with this:

<img src="myimage.png" name="back" id="loadpicture" onclick="aFileIsLoaded()">

but I'm getting this error:

Uncaught TypeError: Cannot read property 'target' of undefined 

any help will be appreciated

Share Improve this question edited Sep 28, 2012 at 0:45 Mikko Ohtamaa 84k61 gold badges288 silver badges468 bronze badges asked Sep 27, 2012 at 19:23 Tony MedinaTony Medina 331 gold badge1 silver badge5 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 2

You are calling a custom function:

function aFileIsLoaded(e1)
{
        var filename = e1.target.files[0];

This function expects an Event object instance, of a file input change event, to be passed as its first and only argument. You do not pass this parameter e1 in your onclick handler

 onclick="aFileIsLoaded()">

The workaround is to use DOM look-up to look the element in the question to read its files

 var filename = document.getElementById('loadpicture').files[0]

As the example lacks HTML code the answer might not be plete.

More information abouts the events

https://developer.mozilla/en-US/docs/DOM/element.addEventListener

发布评论

评论列表(0)

  1. 暂无评论