我正在制作一个图片管理器,我想通过谷歌图片描述。目前我正在尝试将所有图像一次上传到服务器并显示它们,出于某种原因它只显示最后一个选择。
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);
} });
答案 0 :(得分:1)
这是因为variable hoisting in 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);
} });
答案 1 :(得分:0)
您必须在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);