效果图
jfinal controller 源码
//下载
public void download(){
String text = this.getPara("text").replaceAll("/", "\\\\");
String path=JFinal.me().getServletContext().getRealPath("/")+text;//要导出的数据文件
File file = new File(path);
if(file.exists()){//判断文件是否存在
System.out.println("yes ");
this.setAttr("state", "0");
this.renderJson();
return;
}else{
System.out.println("no ");
this.setAttr("state", "FAILED");
this.renderJson();
return ;
}
}
//下载成功
public void downloadOk(){
String text = this.getPara("text").replaceAll("/", "\\\\");
String path=JFinal.me().getServletContext().getRealPath("/")+text;//要导出的数据文件
System.out.println(path);
this.renderFile(new File(path));
}
js 源码
//点击直接下载
function clickDownload(text){
console.log(text);
$.ajax({
url:'${path}/qtxz/download',
type:'POST',
async:false,
data:{'text':text},
dataType:'json',
success:function(obj){
if(obj.state == "0"){
window.open('${path}/qtxz/downloadOk?text='+text);
}else{
alert("文件丢失");
}
},
error:function(obj){
alert("系统错误!");
}
});
}