异步下载文件-html
2021-04-02 01:24
标签:async encoder 禁用 编码 pen xss 表情 下载失败 index 后台接口: 异步下载文件-html 标签:async encoder 禁用 编码 pen xss 表情 下载失败 index 原文地址:https://www.cnblogs.com/lazyli/p/14600204.htmlfunction asyncExportExcel(fileId){
var loadIndex = ‘‘;
var url = jsResPath + "partFileRecord/downloadComResult?fileId="+fileId; //下载文件路径
var xhr=null;
try {
xhr=new XMLHttpRequest()
}catch(e) {
xhr=new ActiveXObject("Microsoft.XMLHTTP")
}
//显示正在下载
loadIndex = layer.load(2, {
shade: [0.5, ‘gray‘], //0.5透明度的灰色背景
content: ‘下载中...‘,
success: function (layero) {
layero.find(‘.layui-layer-content‘).css({
‘padding-top‘: ‘39px‘,
‘width‘: ‘60px‘
});
}
});
xhr.open(‘POST‘, url, true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.responseType = "blob"; // 返回类型blob
// 定义请求完成的处理函数,请求前也可以增加加载框/禁用下载按钮逻辑
xhr.onload = function () {
// 请求完成
if (this.status === 200) {//返回200
//从响应头中获取filename
var filename = this.getResponseHeader(‘filename‘);
//解码
filename =decodeURI(filename);
var blob = this.response;
var reader = new FileReader();
reader.readAsDataURL(blob); //转换为base64,可以直接放入a表情href
reader.onload = function (e) {
// 转换完成,创建一个a标签用于下载
var a = document.createElement(‘a‘);
a.download = filename; //下载的文件名
a.href = e.target.result;
$("body").append(a); //修复firefox中无法触发click
a.click();
$(a).remove();
//关闭正在下载
layer.close(loadIndex);
}
}else {
layer.msg("下载失败!");
}
};
//发送ajax请求
xhr.send();
}
@RequestMapping("/downloadInspectResult")
public void downloadInspectResult(HttpServletResponse response,HttpServletRequest request){
try{
long fileId = Long.valueOf(request.getParameter("fileId"));
PartFileOriginal partFileOriginal = partFileOriginalService.queryRecordById(fileId);
XSSFWorkbook workbook = new XSSFWorkbook();
//1-下载原始编码检查结果
Sheet sheet = workbook.createSheet("10号检查(原始编码)");
downloadDataService.downloadOriginalCodeInspect(fileId,sheet);
String filename = partFileOriginal.getFileName();
//2-下载与6.0比对的结果
Sheet sheet1 = workbook.createSheet("与6.0比对的结果");
downloadDataService.downloadOriginalCodeSixDb(fileId,sheet1);
//3-下载标准编码比对的结果
Sheet sheet2 = workbook.createSheet("标准编码比对结果");
downloadDataService.downloadCriterionCodeInspect(fileId,sheet2 );
//4-下载备注中包含左右,与配件标准名称做对比,若两则备注中包含的左右与标准名称中包含的左右是否一致,不一致提取出来。
Sheet sheet3 = workbook.createSheet("备注中的左右比对结果");
downloadDataService.downloadLeftRightInspect(fileId,sheet3);
response.setContentType("application/force-download");
response.setHeader("Content-Disposition", "attachment;filename=" + java.net.URLEncoder.encode(filename, "UTF-8") + ".xlsx");
response.setHeader("filename", java.net.URLEncoder.encode(filename, "UTF-8") + ".xlsx");
workbook.write(response.getOutputStream());
response.getOutputStream().close();
}catch (Exception e){
LogUtils.error("/dataDeal%downloadPartModel===",e);
}
}