简体   繁体   English

上传前预览图片

[英]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. 未捕获的TypeError:无法读取未定义的属性“ 0”。

What's wrong in my code? 我的代码有什么问题? Please help. 请帮忙。

Most probably, this.files is undefined . 很可能this.filesundefined Thus it's giving the error. 因此,它给出了错误。 Instead of this.files[0] , try t8.files[0] 代替this.files[0] ,尝试t8.files[0]

Chobi is an Image Processing Library , implemented in pure Javascript which I have designed to make such things easier. Chobi是一个图像处理库 ,使用纯Javascript实现,我设计了它来简化此类操作。 You could check that out too 你也可以检查一下

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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