gpt4 book ai didi

javascript - 上传前多张图片预览

转载 作者:行者123 更新时间:2023-11-28 09:49:49 33 4
gpt4 key购买 nike

我想预览多张图片,但问题是我的代码只在上传 2 张图片时有效。我想预览用户上传的内容。

这是我的 javascript :

var abc = 0; //Declaring and defining global increement variable

$(document).ready(function() {


$('#file').click(function() {
$(this).before($("<div/>", {id: 'filediv'}).fadeIn('slow').append(
$("<input/>", {name: 'file[]', type: 'file', id: 'file'}),
$("<br/><br/>")
));
});

$('body').on('change', '#file', function(){
if (this.files && this.files[0]) {
abc += 1; //increementing global variable by 1

var z = abc - 1;
var x = $(this).parent().find('#previewimg' + z).remove();
$(this).before("<div id='abcd"+ abc +"' class='abcd'><img id='previewimg" + abc + "' src=''/></div>");

var reader = new FileReader();
reader.onload = imageIsLoaded;
reader.readAsDataURL(this.files[0]);

$(this).hide();
$("#abcd"+ abc).append($("<img/>", {id: 'img', src: 'x.png', alt: 'delete'}).click(function() {
$(this).parent().parent().remove();
}));
}
});

function imageIsLoaded(e) {
$('#previewimg' + abc).attr('src', e.target.result);
};

$('#upload').click(function(e) {
var name = $(":file").val();
if (!name)
{
alert("First Image Must Be Selected");
e.preventDefault();
}
});
});

这是我的 FIDDLE需要你的帮助来解决它。

最佳答案

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Upload images with Jquery</title>
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
var storedFiles = [];
$('#myfiles').on('change', function() {
$('#messages').html('');
var myfiles = document.getElementById('myfiles');
var files = myfiles.files;
var i=0;
for (i = 0; i<files.length; i++) {
var readImg = new FileReader();
var file=files[i];
if(file.type.match('image.*')){
storedFiles.push(file);
readImg.onload = (function(file) {
return function(e) {
$('#uploadedfiles').append('<tr class="imageinfo"><td><img width="80" height="70" src="'+e.target.result+'"/></td><td>'+file.name+'</td><td>'+Math.round((file.size/1024))+'KB</td><td><a href="" class="lnkcancelimage" file="'+file.name+'" title="Cancel"><img src="delete.png" width=34" height="34"/></a></td></tr>');
};
})(file);
readImg.readAsDataURL(file);
}else{
alert('the file '+file.name+' is not an image<br/>');
}
}
});

$('#uploadedfiles').on('click','a.lnkcancelimage',function(){
$(this).parent().parent().html('');
var file=$(this).attr('file');
for(var i=0;i<storedFiles.length;i++) {
if(storedFiles[i].name == file) {
storedFiles.splice(i,1);
break;
}
}
return false;
});

$('#lnkupload').click(function(){
var data = new FormData();
var i=0;
for(i=0; i<storedFiles.length; i++) {
data.append('files'+i, storedFiles[i]);
}

if(i>0){
$.ajax({
url: 'load.php',
type: 'POST',
contentType: false,
data: data,
processData: false,
cache: false
}).done(function(msg) {
storedFiles = [];
if(msg){
alert(msg);
}else{
$('#messages').html('Images uploaded successfully');
}
}).fail(function() {
alert('error');
});
}
return false;
});

});
</script>
</head>
<body>
<div id="wrapper">
<label><strong>Uploading multiple images</strong></label>
<input id="myfiles" type="file" name="myfiles[]" multiple="multiple" />
<a href="" id="lnkupload">Upload</a>
<table id="uploadedfiles">
<tr><th>Image</th><th>Name</th><th>Size</th><th>Actions</th></tr>
</table>
</div>
</body>
</html>

关于javascript - 上传前多张图片预览,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25129955/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com