<html>
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no,
initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="./jquery.min.js"></script>
<style>
.dropzone {
border: 2px dashed #0087F7;
border-radius: 5px;
background: white;
display: flex;
flex-wrap: wrap;
}
.file-item {
width: 100px;
margin: 20px;
height: 150px;
overflow: hidden;
border: 1px solid #666;
text-align: center;
position: relative;
}
.file-item button {
width: 100%;
height: 20px;
position: absolute;
bottom: 0;
left: 0;
background: none;
border: none;
background-color: #ff4338;
color: #fff;
border-top: 1px solid #333;
}
.file-item img {
width: 100px;
height: 100px;
object-fit: cover;
}
</style>
</head>
<body>
<div id="factory_file" class="tab-pane">
<div class="tips" style="color:#ff0000">限制10个文件</div>
<div id="div" class="dropzone" ondrop="drop(event)"
ondragover="allowDrop(event)" ondragleave="removeDrop(event)"
style="min-height: 260px;">
</div>
<div id="save_files"></div>
</div>
</body>
<script>
//定义要上传的问价数组
var uploadFiles = new Array();
//文件移入
function allowDrop(event) {
event.preventDefault(); //阻止浏览器默认事件
}
//文件移除
function removeDrop(event){
event.preventDefault();
}
//当鼠标松开文件
function drop(event) {
event.preventDefault();
var files = event.dataTransfer.files
if(files.length==0){ //没有文件返回false
return false
}
console.log(files);
for(var i=0; i<files.length; i++) {
let file = files[i]
let type = ''
// 是图片
if(file.type.indexOf('image/') !== -1) {
type = 'image'
uploadFiles.push({
type, type,
imgsrc: window.URL.createObjectURL(file),
name: file.name,
file:file,
})
} else {
type = 'pdf'
uploadFiles.push({
type: type,
imgsrc: '',
name: file.name,
file:file,
})
}
}
renderFileList(uploadFiles);
}
function renderFileList(list) {
let html = ''
for(var i=0; i<list.length; i++) {
var item = list[i]
if(item.type === 'image') {
html += `<div class="file-item"><img src="${item.imgsrc}" />
<p>${item.name}</p><button onclick="del(${i})">删除</button></div>`
} else {
html += `<div class="file-item">PDF<p>${item.name}</p>
<button onclick="del(${i})">删除</button></div>`
}
}
document.querySelector('#div').innerHTML = html;
}
function del(index) {
uploadFiles.splice(index, 1);
renderFileList(uploadFiles);
}
$('#sub_btn').click(function () {
var formdata = new FormData(document.querySelector("form"));
if(uploadFiles.length > 10){
alert('超出上传文件数量');
return false;
}
for(var j=0; j< uploadFiles.length; j++) {
formdata.append('files'+j,uploadFiles[j].file)
}
$.ajax({
type: "POST",
url: "路由",
data: formdata,
contentType: false,
processData: false,
dataType: 'json',
success: function (data) {
alert(data.msg)
if (data.code == 0) {
setTimeout(function () {
window.location.href = "路由";
}, 1000)
}
}
});
});
</script>
</html>