我正在制作一个图像组织者,我想通过谷歌图像描述。目前,我试图一次将所有图像上传到服务器并显示它们,由于某种原因,它只显示最后的选择。
HTML<input type='file' accept='image/*' multiple onchange="readURL(this);"/>
<div id="images-container"></div>
<label><strong>(Image will display above)</strong></label>
CSS #blah {
display: block;
}
img {
width:100%;
}
.picContainer {
border: 1px dashed black;
margin: 10px;
padding: 10px;
width: 100px;
display: inline-block
}
JS
$("input").change(function(e) {
for (var i = 0; i < e.originalEvent.srcElement.files.length; i++) {
var file = e.originalEvent.srcElement.files[i];
var div = document.createElement("div");
var img = document.createElement("img");
div.className = "picContainer";
var reader = new FileReader();
reader.onloadend = function() {
img.src = reader.result;
}
reader.readAsDataURL(file);
div.appendChild(img);
//$("input").after(div);
var container = document.getElementById("images-container");
container.appendChild(div);
} });
这是因为javascript中的变量提升
编译器会重写你的代码,使它看起来像这样:
$("input").change(function(e) {
var i, file, div, img, container;
for (i = 0; i < e.originalEvent.srcElement.files.length; i++) {
file = e.originalEvent.srcElement.files[i];
div = document.createElement("div");
img = document.createElement("img");
div.className = "picContainer";
reader = new FileReader();
reader.onloadend = function() {
img.src = reader.result;
}
reader.readAsDataURL(file);
div.appendChild(img);
//$("input").after(div);
container = document.getElementById("images-container");
container.appendChild(div);
} });
这对你来说意味着,在函数
中reader.onloadend = function() {
img.src = reader.result;
}
读取器和img变量总是等于最后一个(即使是先初始化的)
你可能应该使用在循环之前定义的函数创建器,像这样:
$("input").change(function(e) {
function initOnReaderLoaded(img, reader){
return function(){
img.src = reader.result;
}
}
for (var i = 0; i < e.originalEvent.srcElement.files.length; i++) {
var file = e.originalEvent.srcElement.files[i];
var div = document.createElement("div");
var img = document.createElement("img");
div.className = "picContainer";
var reader = new FileReader();
reader.onloadend = initOnReaderLoaded(img, reader);
reader.readAsDataURL(file);
div.appendChild(img);
//$("input").after(div);
var container = document.getElementById("images-container");
container.appendChild(div);
} });
你必须在reader onload中创建你的img元素。
小提琴:
function handleFileSelect(evt) {
var files = evt.target.files; // FileList object
// Loop through the FileList and render image files as thumbnails.
for (var i = 0, f; f = files[i]; i++) {
// Only process image files.
if (!f.type.match('image.*')) {
continue;
}
var reader = new FileReader();
// Closure to capture the file information.
reader.onload = (function(theFile) {
return function(e) {
// Render thumbnail.
var span = document.createElement('span');
span.innerHTML = ['<img class="thumb" src="', e.target.result,
'" title="', escape(theFile.name), '"/>'].join('');
document.getElementById('list').insertBefore(span, null);
};
})(f);
// Read in the image file as a data URL.
reader.readAsDataURL(f);
}
}
document.getElementById('files').addEventListener('change', handleFileSelect, false);
小提琴