简体   繁体   中英

Preview Image before upload

I have a table. Each row has one cell for image uploading. I want to display the preview. For this I wrote the code below:

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);

While running it gives me an error:

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

What's wrong in my code? Please help.

Most probably, this.files is undefined . Thus it's giving the error. Instead of this.files[0] , try t8.files[0]

Chobi is an Image Processing Library , implemented in pure Javascript which I have designed to make such things easier. You could check that out too

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM