使用ReactJS,我需要多个图片上传并预览。我尝试使用下面的代码,但它不起作用,控制台显示错误信息:
Uncaught DOMException: Failed to set the 'value' property on 'HTMLInputElement': This input element accepts a filename, which may only be programmatically set to the empty string.
Uncaught DOMException: Failed to set the 'value' property on 'HTMLInputElement': This input element accepts a filename, which may only be programmatically set to the empty string.
uploadImage(e){
var numFiles = e.target.files.length;
for (var i = 0, numFiles = e.target.files.length; i < numFiles; i++){
var file = e.target.files[i];
if(!file.type.match('image'))
continue;
var reader = new FileReader();
reader.onload = function(e) {
setOfImages.push({
fileName:file.name,
image:e.target.result.split(',')[1]
});
for(var j = 0; j<setOfImages.length; j++) {
$('.placeholder-blk').prepend('<div class="image-placeholder"><img src="data:image/jpeg;base64,'+ setOfImages[j].image +'" /></div>');
}
console.log(setOfImages);
}
reader.readAsDataURL(file);
}
}
<input type="file" name="image-uploder" id="image-uploder" className="file-loader" onChange={this.uploadImage.bind(this)} multiple />
$('.placeholder-blk').remove();
,然后随后又试图在其前面添加?它已经不存在了。 - Mark Schultheissvar file = e.target.files[0];
你是不是想处理多个文件var numFiles = e.target.files.length; for (var i = 0, numFiles = e.target.files.length; i < numFiles; i++) { var file = e.target.files[i]; .. }
- Mark Schultheiss