2016-09-20 66 views
0

我有一张桌子。每行有一个用于图像上传的单元格。我想显示预览。为此,我写了下面的代码:上传之前的预览图

var cell8 = row.insertCell(6); 
var t8 = document.createElement("input"); 
t8.type = "file"; 
t8.id = "img" + index; 
t8.name = "img" + index; 

var a = document.getElementById('blah_img' + index + ''); 
a.src = window.URL.createObjectURL(this.files[0]); 
t8.onchange = a.src; 
cell8.appendChild(t8); 

var oImg = document.createElement("img"); 
oImg.setAttribute('src', 'noimg.png'); 
oImg.setAttribute('alt', 'your image'); 
oImg.setAttribute('height', '100'); 
oImg.setAttribute('width', '100'); 
oImg.id = "blah_img" + index; 
cell8.appendChild(oImg); 

在运行它给了我一个错误:

Uncaught TypeError: Cannot read property '0' of undefined.

什么是错在我的代码?请帮忙。

+0

哪条线给出了这个错误? –

+0

window.URL.createObjectURL(this.files [0]) – Rose

+1

什么是'this'旨在引用?尝试't8.files [0]' –

回答

0

最有可能的是,this.filesundefined。因此它给出了错误。 相反的this.files[0],尝试t8.files[0]

Chobi的图像处理库,在我所设计,使这样的事情更容易纯JavaScript实现的。你也可以检查出来

+0

这与OP的问题无关,它是边缘自我推销垃圾邮件。 –

+0

我编辑了我的答案,包含一个可能的解决方案 –

+0

虽然将其更改为t8,它显示'Uncaught TypeError:'URL'上执行'createObjectURL'失败:找不到与提供的签名相匹配的函数' – Rose

相关问题