实现前端文件下载
java代码
此处,是将要调用的后台代码后,返回到前台是文件内容
格式如下:
//查看-导出Licence
$(".viewButton").on("click","#exportLicence",function(){
var moduleInfo = [];//定义一个空数组
$("input[type='checkbox']:checked").each(function(i){//把所有被选中的复选框的值存入数组
var datamodulename=$(this).siblings("span").text();
var datamoduleid=$(this).siblings("span")[0].dataset.moduleid;
var datatemp={};
datatemp.moduleid=datamoduleid
datatemp.modulename=datamodulename
moduleInfo[i] =datatemp;
// console.log("applyModuleInfo==",applyModuleInfo)
});
var startTime=$("#need_startTime").val();
$.ajax({
url: $treeBasePath+"/authorize/exportLicense",
data: {
"queryStr":JSON.stringify({
"productInfo":{
"startTime":startTime,
"endTime":$("#need_endTime").val(),
"productNo":$("#need_productNo").val(),
"productVersion":$("#vproductVersion").text()
},
moduleInfo
})
},
method:"POST",
datatype: "text/plain;charset=utf-8",
async: false,
success: function (data) {
downFile(data, 'license.txt');
}
});
});
function downFile(content,filename) {
var eleLink = document.createElement('a');
eleLink.download = filename;
eleLink.style.display = 'none';
// 字符内容转变成blob地址
var blob = new Blob([content]);
eleLink.href = URL.createObjectURL(blob);
// 触发点击
document.body.appendChild(eleLink);
eleLink.click();
// 然后移除
document.body.removeChild(eleLink);
};
点击下载即可下载至浏览器默认路径下
ps:如果朋友们有什么好的思路或者方法,还望不吝赐教,留言评论^_^