admin管理员组文章数量:1277901
My html code is like this :
<input type='file' multiple/>
<img id="myImg" src="#" alt="your image" />
My javascript code is like this :
$(function () {
$(":file").change(function () {
if (this.files && this.files[0]) {
var reader = new FileReader();
reader.onload = imageIsLoaded;
reader.readAsDataURL(this.files[0]);
}
});
});
function imageIsLoaded(e) {
$('#myImg').attr('src', e.target.result);
};
Demo is like this : /
When I upload one image, it display the image
But when I upload more than 1 image, shown only one image
How can I solve this problem?
My html code is like this :
<input type='file' multiple/>
<img id="myImg" src="#" alt="your image" />
My javascript code is like this :
$(function () {
$(":file").change(function () {
if (this.files && this.files[0]) {
var reader = new FileReader();
reader.onload = imageIsLoaded;
reader.readAsDataURL(this.files[0]);
}
});
});
function imageIsLoaded(e) {
$('#myImg').attr('src', e.target.result);
};
Demo is like this : https://jsfiddle/oscar11/ob8aas23/
When I upload one image, it display the image
But when I upload more than 1 image, shown only one image
How can I solve this problem?
Share Improve this question asked May 19, 2017 at 4:26 samuel tohsamuel toh 7,08624 gold badges76 silver badges110 bronze badges 2-
1
simultaneously? try having one
<img>
tag per image? – Jaromanda X Commented May 19, 2017 at 4:30 - the code will always show only one image. you have only one image tag – karthick Commented May 19, 2017 at 4:31
5 Answers
Reset to default 6The issue is there is only one img
tag but for multiple images to be displayed , equal number of img
tags will be required. So it will be better to to loop through the files.length
and dynamically create a image tag and add source to it
$(function() {
$(":file").change(function() {
if (this.files && this.files[0]) {
for (var i = 0; i < this.files.length; i++) {
var reader = new FileReader();
reader.onload = imageIsLoaded;
reader.readAsDataURL(this.files[i]);
}
}
});
});
function imageIsLoaded(e) {
$('#myImg').append('<img src=' + e.target.result + '>');
};
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type='file' multiple/>
<div id="myImg">
</div>
The first issue is when you select multiple sources you are not iterating the list.
The second issue is there is only one img tag where the result is displayed.
I have updated the code
<input type='file' multiple/>
<div class="img-container">
</div>
$(function () {
$(":file").change(function () {
/*if (this.files && this.files[0]) {
var reader = new FileReader();
reader.onload = imageIsLoaded;
reader.readAsDataURL(this.files[0]);
}*/
var noOfFiles = this.files.length;
for(var i=0; i < noOfFiles; i++){
var reader = new FileReader();
reader.onload = imageIsLoaded;
reader.readAsDataURL(this.files[i]);
}
});
});
function imageIsLoaded(e) {
var imgTmpl = '<img src='+e.target.result+'>';
$('.img-container').append(imgTmpl);
};
jsfiddle for the same
https://jsfiddle/karthick6891/ob8aas23/1/
To display multipal image you must loop through the file and append img tag to display multi-pal file.
var selDiv = "";
document.addEventListener("DOMContentLoaded", init, false);
function init() {
document.querySelector('#files').addEventListener('change', handleFileSelect, false);
selDiv = document.querySelector("#selectedFiles");
}
function handleFileSelect(e) {
if (!e.target.files || !window.FileReader) return;
selDiv.innerHTML = "";
var files = e.target.files;
var filesArr = Array.prototype.slice.call(files);
filesArr.forEach(function(f,i) {
var f = files[i];
if (!f.type.match("image.*")) {
return;
}
var reader = new FileReader();
reader.onload = function(e) {
var html = "<img src=\"" + e.target.result + "\">" + f.name + "<br clear=\"left\"/>";
selDiv.innerHTML += html;
}
reader.readAsDataURL(f);
});
}
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="file" id="files" name="files" multiple accept="image/*"><br/>
<div id="selectedFiles"></div>
Your code was setup only for one image, so only one was displayed. Make sure you dynamically add new images when the upload happens. Here is example (also on https://jsfiddle/ob8aas23/3/ ):
HTML
<input type='file' multiple/>
JS
$(function () {
$(":file").change(function () {
if (this.files) {
for(var i = 0, len = this.files.length; i<len ; i++) {
var reader = new FileReader();
reader.onload = imageIsLoaded;
reader.readAsDataURL(this.files[i]);
}
}
});
});
function imageIsLoaded(e) {
var newImage = $( '<img id="myImg" alt="your image" />' )
newImage.attr('src', e.target.result)
$('body').append(newImage)
}
$(function() {
$(":file").change(function() {
if (this.files && this.files[0]) {
for (var i = 0; i < this.files.length; i++) {
var reader = new FileReader();
reader.onload = imageIsLoaded;
reader.readAsDataURL(this.files[i]);
}
}
});
});
function imageIsLoaded(e) {
$('#myImg').append('<img src=' + e.target.result + '>');
};
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type='file' multiple/>
<div id="myImg">
</div>
本文标签: javascriptHow can I display multiple imageStack Overflow
版权声明:本文标题:javascript - How can I display multiple image? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741297311a2370891.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论