下载模板按钮
<div class="floatLeft"
style="margin: 0 0 0 36px; background-color: #58aefd; color: #000000; border: 1px solid #4facff;
width: 70px; height: 28px;" onclick="downloadMould()">
下载模板
</div>
把上面的div放在body里面,然后样式自己网上找一些好看的设置,重点是绑定的事件downloadMould
下载事件jsp前端
function downloadFile() {
$.ajax({
type: "POST",
url: "/test/exist",
data: "time=" + new Date().getTime(),
success: function (msg) {
msg = jQuery.parseJSON(msg);
if (msg.exist == "true") {
$("body").append(
"<div id='div202405091707' style='display: none;'>"
+ "<form id='form202405091707' action='"
+ "/test/download"
+ "' method='post'></form>" + "</div>");
$("#form202405091707").submit();
$("#div202405091707").remove();
} else {
$.messager.alert('提示', '文件不存在!', 'info');
}
},
error: ajaxErrorInfo
});
}
这个的前提是默认你已经引入了jQuery和vue,然后注意form表单提交的url,是test/download,这个路径要和你后端requestmapping处理下载的请求一致
后端处理逻辑
@RequestMapping("test/download")
public void download(HttpServletRequest request,HttpServletResponse response){
String filePath= PropertiesUtils.getValue("filePath");
logger.info("filePath="+filePath);
try {
FileDownloadUtil.download(filePath,"",request,response);
} catch (Exception e) {
logger.error("download error",e);
}
}
配置文件application.properties
filePath = C:\\Users\\mi\\Desktop\\downloadModel.xlsx
这个路径就是你服务器存放模板的路径