2016-09-20 96 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