console
var input = document.getElementById('add_file');
input.addEventListener('change',readFile,false);
function bytesToSize(bytes) {
if (bytes === 0) return '0 B';
var k = 1000,
sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'],
i = Math.floor(Math.log(bytes) / Math.log(k));
return (bytes / Math.pow(k, i)).toPrecision(3) + ' ' + sizes[i];
}
function readFile(){
var file = this.files[0];
if(file == undefined){
canLoad = false;
return;
}
canLoad = true;
kamName = file.name;
}
$('.send_btn button ').click(function(){
if($("#add_file").val() == ""){
$('body').toast({
content:"文件不能为空",
duration:2000,
isCenter:false,
background:'rgba(230,0,0,0.5)',
animateIn:'bounceIn-hastrans',
animateOut:'bounceOut-hastrans',
});
return false;
}else{
if (canLoad) {
var file=new FormData($('#defaultForm')[0]);
$.ajax({
url: window.zd_conf.base_url+ 'upload-file/upload',
type: 'POST',
dataType:'json',
cache: false,
data: [{file},{free_service:arr}],
processData: false,
contentType: false,
success:function(res) {
$('body').toast({
content:res.msg,
duration:2000,
isCenter:false,
background:'#4EA44E',
animateIn:'bounceIn-hastrans',
animateOut:'bounceOut-hastrans',
});
},
error:function(){
$('body').toast({
content:"数据访问错误",
duration:2000,
isCenter:false,
background:'rgba(230,0,0,0.5)',
animateIn:'bounceIn-hastrans',
animateOut:'bounceOut-hastrans',
});
}
})
}
}
})
<form method="post" id="defaultForm" enctype="multipart/form-data">
<div class="send_title">
<label>
<input style="position:absolute;opacity:0;" type="file" name="file" id="add_file" accept="image/gif,image/jpeg,image/x-png"/>
<img style="width: 40px;height: 40px;margin-top:20px;" src="img/send.png">
<span style="margin-top:28px;margin-left:16px;position: absolute;">上传列表</span>
</label>
</div>
</form>
button{
border:1px solid #ccc;
cursor:pointer;
display:block;
margin:auto;
position:relative;
top:100px;
}