1.下载ajaxfileupload.js
2.js源码:
<script src="/medias/ajaxfileupload.js" type="text/javascript"></script>
<style type="text/css">
#loading {
background-color:gray;
position:fixed;
left:0;
top:0;
width:100%;
height:100%;
z-index:100000;
opacity:0.2;
filter:alpha(opacity=20);
-moz-opacity:0.2;
-khtml-opacity:0.2;
vertical-align:middle;
}
#loading div {
position:absolute;
left:40%;
top:50%;
}
#loading div span {
color:#000000;
font-weight:700;
}
</style>
<script type="text/javascript">
function uploadSubmit(){
var file = $('#fileToUpload').val();
//检查是否已选择上传文件
if (file != '') {
var filename = file.replace(/.*(\/|\\)/, '');
var fileext = (/[.]/.exec(filename)) ? /[^.]+$/.exec(filename.toLowerCase()) : '';
//检查文件格式
if (fileext == 'xlsx' || fileext == 'xls') {
//展示等待信息
$('#loading').ajaxStart(function(){
$(this).show();
}).ajaxComplete(function(){
$(this).hide();
});
//上传excel文件
$.ajaxFileUpload({
url: 自己的url,
secureuri: false,
dataType: "text",
fileElementId: 'fileToUpload',
success: function(data){ //data是后台返回过来的在上传并保持excel内容过程中的用户提示信息
$("#result").html(data);
$("#result").dialog("open");
},
});
}
else {
alert('文件格式必须是*.xls或*.xlsx');
}
}
else {
alert('请选择excel文件!')
}
};
//初始化用户提示窗口
$(function(){
$("#result").dialog({
autoOpen: false,
height: 140,
modal: true,
buttons: {
Ok: function(){
$(this).dialog("close");
}
}
});
});
</script>
3.html源码
<form name="form" action="" method="POST" enctype="multipart/form-data">
<input id="fileToUpload" type="file" size="45" name="fileToUpload"> <input type="button" id="buttonUpload" onclick="return uploadSubmit();" value="导入excel" />
</form>
<br>
<br>
<div id = 'result'></div>
<div id='loading' style="display:none;">
<div><span style="color:#000000;">正在操作,请稍候... ...</span></div>
</div>
来源:https://www.cnblogs.com/cathouse/archive/2012/11/19/2776978.html