JQuery多图片上传+预览
参考自
这是CSS部分代码
.float
{
float
:left
;
width
: 200px
;
height
: 200px
;
overflow
: hidden
;
border
: 1px solid #
CCCCCC;
border
-radius
: 10px
;
padding
: 5px
;
margin
: 5px
;
}
img
{
position
: relative
;
}
.result
{
width
: 200px
;
height
: 200px
;
text
-align
: center
;
box
-sizing
: border
-box
;
}
#file_input
{
}
.delete{
width
: 200px
;
height
:200px
;
position
: absolute
;
text
-align
: center
;
line
-height
: 200px
;
z
-index
: 10;
font
-size
: 30px
;
background
-color
: rgba(255,255,255,0.8);
color
: #
777;
opacity
: 0;
transition
-duration
: 0.7s
;
-webkit
-transition
-duration
: 0.7s
;
}
.delete:hover
{
cursor
: pointer
;
opacity
: 1;
}
这是body部分
<div
class="container">
<label
>图片选择:
</label
>
<button id
="select">更换图片
</button
>
<button id
="add">追加图片
</button
>
<input type
="file" id
="file_input" multiple
/> <!--multiple是多选的关键
-->
<button id
="submit">提交
</button
>
</div
>
<div
class="get"></div
>
这是引入的依赖
<script src
="https://code.jquery.com/jquery-3.4.1.min.js"></script
>
这是js部分
$(function(){
var input
= $("#file_input");
var result
;
var dataArr
= [];
var fd
;
var oSelect
= $("#select");
var oAdd
= $("#add");
var oSubmit
= $("#submit");
var oInput
= $("#file_input");
if(typeof FileReader
==='undefined'){
alert("抱歉,你的浏览器不支持 FileReader");
input
.setAttribute('disabled','disabled');
}else{
$("#file_input").on('change',readFile
);
}
function readFile(){
fd
= new FormData();
var iLen
= this.files
.length
;
var path
=$("#file_input").val();
var extStart
=path
.lastIndexOf('.');
var ext
= path
.substring(extStart
,path
.length
).toUpperCase();
for(var i
=0;i
<iLen
;i
++){
if (ext
!== '.PNG' && ext
!== '.JPG' && ext
!== '.JPEG' && ext
!== '.GIF'){
return alert("上传的图片格式不正确,请重新选择");
}
var reader
= new FileReader();
fd
.append(i
,this.files
[i
]);
reader
.readAsDataURL(this.files
[i
]);
reader
.fileName
= this.files
[i
].name
;
reader
.onload = function(e
){
var imgMsg
= {
name
: this.fileName
,
base64
: this.result
}
dataArr
.push(imgMsg
);
result
= '<div class="delete">删除</div><div class="result"><img class="subPic" src="'+this.result
+'" alt="'+this.fileName
+'"/></div>';
var div
= document
.createElement('div');
div
.innerHTML
= result
;
div
['className'] = 'float';
$(".get")[0].append(div
);
var img
= div
.getElementsByTagName('img')[0];
img
.onload = function(){
var nowHeight
= ReSizePic(this);
this.parentNode
.style
.display
= 'block';
var oParent
= this.parentNode
;
if(nowHeight
){
oParent
.style
.paddingTop
= (oParent
.offsetHeight
- nowHeight
)/2 + 'px';
}
}
div
.onclick = function(){
$(this).remove();
}
}
}
}
function send(){
var submitArr
= [];
$('.subPic').each(function () {
submitArr
.push({
name
: $(this).attr('alt'),
base64
: $(this).attr('src')
});
}
);
$
.ajax({
url
: 'http://123.206.89.242:9999',
type
: 'post',
data
: JSON.stringify(submitArr
),
dataType
: 'json',
success
: function(data
){
console
.log('返回的数据:'+JSON.stringify(data
))
}
})
}
$("#select").click(function(){
oInput
.value
= "";
$('.float').remove();
oInput
.click();
})
$("#add").click(function(){
oInput
.value
= "";
oInput
.click();
})
$("#submit").click(function(){
if(!dataArr
.length
){
return alert('请先选择文件');
}
send();
})
} )
function ReSizePic(ThisPic
) {
var RePicWidth
= 200;
var TrueWidth
= ThisPic
.width
;
var TrueHeight
= ThisPic
.height
;
if(TrueWidth
>TrueHeight
){
var reWidth
= RePicWidth
;
ThisPic
.width
= reWidth
;
var nowHeight
= TrueHeight
* (reWidth
/TrueWidth
);
return nowHeight
;
}else{
var reHeight
= RePicWidth
;
ThisPic
.height
= reHeight
;
}
}
接下来是全部的代码
<!DOCTYPE html
>
<html
>
<head
>
<meta charset
="UTF-8">
<title
>showImages
</title
>
<style type
="text/css">
.float
{
float
:left
;
width
: 200px
;
height
: 200px
;
overflow
: hidden
;
border
: 1px solid #
CCCCCC;
border
-radius
: 10px
;
padding
: 5px
;
margin
: 5px
;
}
img
{
position
: relative
;
}
.result
{
width
: 200px
;
height
: 200px
;
text
-align
: center
;
box
-sizing
: border
-box
;
}
#file_input
{
}
.delete{
width
: 200px
;
height
:200px
;
position
: absolute
;
text
-align
: center
;
line
-height
: 200px
;
z
-index
: 10;
font
-size
: 30px
;
background
-color
: rgba(255,255,255,0.8);
color
: #
777;
opacity
: 0;
transition
-duration
: 0.7s
;
-webkit
-transition
-duration
: 0.7s
;
}
.delete:hover
{
cursor
: pointer
;
opacity
: 1;
}
</style
>
</head
>
<body
>
<div
class="container">
<label
>添加图片:
</label
>
<button id
="select">更新图片
</button
>
<button id
="add">继续添加
</button
>
<input type
="file" id
="file_input" multiple
/>
<button id
="submit">提交
</button
>
</div
>
<div
class="get"></div
>
</body
>
<script src
="https://code.jquery.com/jquery-3.4.1.min.js"></script
>
<script type
="text/javascript">
$(function(){
var result
;
var dataArr
= [];
var fd
;
var oInput
= $("#file_input");
if(typeof FileReader
==='undefined'){
alert("抱歉,你的浏览器不支持 FileReader");
input
.setAttribute('disabled','disabled');
}else{
$("#file_input").on('change',readFile
);
}
function readFile(){
fd
= new FormData();
var iLen
= this.files
.length
;
var path
=$("#file_input").val();
var extStart
=path
.lastIndexOf('.');
var ext
= path
.substring(extStart
,path
.length
).toUpperCase();
for(var i
=0;i
<iLen
;i
++){
if (ext
!== '.PNG' && ext
!== '.JPG' && ext
!== '.JPEG' && ext
!== '.GIF'){
return alert("上传的图片格式不正确,请重新选择");
}
var reader
= new FileReader();
fd
.append(i
,this.files
[i
]);
reader
.readAsDataURL(this.files
[i
]);
reader
.fileName
= this.files
[i
].name
;
reader
.onload = function(e
){
var imgMsg
= {
name
: this.fileName
,
base64
: this.result
}
dataArr
.push(imgMsg
);
result
= '<div class="delete">删除</div><div class="result"><img class="subPic" src="'+this.result
+'" alt="'+this.fileName
+'"/></div>';
var div
= document
.createElement('div');
div
.innerHTML
= result
;
div
['className'] = 'float';
$(".get")[0].append(div
);
var img
= div
.getElementsByTagName('img')[0];
img
.onload = function(){
var nowHeight
= ReSizePic(this);
this.parentNode
.style
.display
= 'block';
var oParent
= this.parentNode
;
if(nowHeight
){
oParent
.style
.paddingTop
= (oParent
.offsetHeight
- nowHeight
)/2 + 'px';
}
}
div
.onclick = function(){
$(this).remove();
}
}
}
}
function send(){
var submitArr
= [];
$('.subPic').each(function () {
submitArr
.push({
name
: $(this).attr('alt'),
base64
: $(this).attr('src')
});
}
);
$
.ajax({
url
: 'http://123.206.89.242:9999',
type
: 'post',
data
: JSON.stringify(submitArr
),
dataType
: 'json',
success
: function(data
){
console
.log('返回的数据:'+JSON.stringify(data
))
}
})
}
$("#select").click(function(){
oInput
.value
= "";
$('.float').remove();
oInput
.click();
})
$("#add").click(function(){
oInput
.value
= "";
oInput
.click();
})
$("#submit").click(function(){
if(!dataArr
.length
){
return alert('请先选择文件');
}
send();
})
} )
function ReSizePic(ThisPic
) {
var RePicWidth
= 200;
var TrueWidth
= ThisPic
.width
;
var TrueHeight
= ThisPic
.height
;
if(TrueWidth
>TrueHeight
){
var reWidth
= RePicWidth
;
ThisPic
.width
= reWidth
;
var nowHeight
= TrueHeight
* (reWidth
/TrueWidth
);
return nowHeight
;
}else{
var reHeight
= RePicWidth
;
ThisPic
.height
= reHeight
;
}
}
</script
>
</html
>
写的第一条博客,谢谢支持
转载请注明原文地址: https://win8.8miu.com/read-16280.html