<!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{
display: none;
}
.
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>
<script src=
"http://libs.baidu.com/jquery/2.0.0/jquery.min.js"
></script>
<script type=
"text/javascript"
>
window.onload =
function
(){
var
input = document.getElementById(
"file_input"
);
var
result;
var
dataArr = [];
var
fd;
var
oSelect = document.getElementById(
"select"
);
var
oAdd = document.getElementById(
"add"
);
var
oSubmit = document.getElementById(
"submit"
);
var
oInput = document.getElementById(
"file_input"
);
if
(
typeof
FileReader===
'undefined'
){
alert(
"抱歉,你的浏览器不支持 FileReader"
);
input.setAttribute(
'disabled'
,
'disabled'
);
}
else
{
input.addEventListener(
'change'
,readFile,
false
);
}
function
readFile(){
fd =
new
FormData();
var
iLen =
this
.files.length;
var
index = 0;
for
(
var
i=0;i<iLen;i++){
if
(!input[
'value'
].match(/.jpg|.gif|.png|.jpeg|.bmp/i)){
return
alert(
"上传的图片格式不正确,请重新选择"
);
}
var
reader =
new
FileReader();
reader.index = i;
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">delete</div><div class="result"><img src="'
+
this
.result+
'" alt=""/></div>'
;
var
div = document.createElement(
'div'
);
div.innerHTML = result;
div[
'className'
] =
'float'
;
div[
'index'
] = index;
document.getElementsByTagName(
'body'
)[0].appendChild(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();
delete
dataArr[
this
.index];
}
index++;
}
}
}
function
send(){
var
submitArr = [];
for
(
var
i = 0; i < dataArr.length; i++) {
if
(dataArr[i]) {
submitArr.push(dataArr[i]);
}
}
$.ajax({
url :
'http://39.106.182.218'
,
type :
'post'
,
data : JSON.stringify(submitArr),
dataType:
'json'
,
success :
function
(data){
console.log(
'返回的数据:'
+JSON.stringify(data))
}
})
}
oSelect.οnclick=
function
(){
oInput.value =
""
;
$(
'.float'
).remove();
dataArr = [];
index = 0;
oInput.click();
}
oAdd.οnclick=
function
(){
oInput.value =
""
;
oInput.click();
}
oSubmit.οnclick=
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>
</head>
<body>
<div class=
"container"
>
<label>请选择一个图像文件:</label>
<button id=
"select"
>(重新)选择图片</button>
<button id=
"add"
>(追加)图片</button>
<form action=
""
method=
"post"
enctype=
"multipart/form-data"
>
<input type=
"file"
id=
"file_input"
name=
"image[]"
multiple/>
<!--用input标签并选择type=file,记得带上multiple,不然就只能单选图片了-->
<button id=
"submit"
>提交</button>
</form>
</div>
</body>
</html>