使用easyui进行文件上传
HTML代码
<form id ="uploadform" style="display:inline-flex;">
<input class="easyui-filebox" name="file" id = "file" onfocus=this.blur() data-options="prompt:'Choose a file...'" style="width:100%;">
<input name="_tf_token_" type="hidden" value="" id="tr_token"/>
<input type="button" value="导入" onclick="doUpload()"
class="button"/>
</form>
Javascript代码
function doUpload() {
$('#tr_token').val(tokenValue); // 获取全局token值
var file = $( "#filebox_file_id_1" ).val(); // filebox_file_id_1 是easyui生成的id
// 判断上传的文件格式
var filetype = file.substr(file.length-4,file.length);
if(!(filetype.indexOf("csv")>0)){
alertMsg("上传的文件格式出错,请按照模板上传");
return;
}
/// 获取文件数据
var formData = new FormData($( "#uploadform" )[0]);
console.log(formData);
$('#dataGrid').datagrid("loading");
if(file){
alertMsg("确认导入文件?", "", null, function(){
$.ajax({
url: '../AllLimitManageC/addImportRiskBlacklist',
type: 'POST',
data:formData,
dataType : "json",
async: false,
cache: false,
contentType: false,
processData: false,
success: function (data) {
var data = formatJson(globalHtmlCharDecode(data));
if(data.result =="success"){
alertMsg(data.msg);
$('#dg').datagrid('loadData',data);
$("#dg").datagrid("loaded"); //移除屏蔽
}else{
alertMsg(data.msg, "", null, null);
$("#dg").datagrid('loadData', {total:0,rows:[]});
$("#detailPanelIndividual").panel('close');
$("#detailPanelCompany").panel('close');
$("#formWindow").window('close');
}
},
error: function (returndata) {
alertMsg(data.msg);
}
});
});
} else {
alertMsg("请先选择文件", "", null, null);
}
}
前端页面展示
操作 :
下载 :
// 下载列表
function downloadTable(tablename){
var arr = tablename.split("_");
var downfilename = "monthaccount"+"_"+arr[1]+".xls";
var _url = [];
var downurl = "../daliyMainc/downloadExcel"
_url.url = downurl;
_url.dataType = "json";
_url.contentType= "application/x-www-form-urlencoded; charset=utf-8";
_url.type = "post";
_url.data = {
"downfilename":downfilename,
};
_url.callback = function (data){
if(data.result == "success"){
alertMsg("下载成功");
} else {
alertMsg(data.msg);
}
};
_url.errorCallback = function(data){
if(data.readyState == 4 && data.status == 200){
// 使用post请求最后还会走这个。。。奇怪!
window.location.href= downurl + "?downfilename="+downfilename;
}else{
alertMsg("系统异常");
}
};
getEasyUiJson(_url);
}