采用ajax方式进行下载 发现行不通
/*下载模板文件*/
function projectreview_down(title,url){
$.ajax(
{
url:url,
method:'get',
success:function (flag) {
alert(flag.code);
//location.replace(location.href);
}
}
);
}
请直接使用
<span class="f-l">
<a href="/projectreview/downtemplate" class="btn btn-primary radius"><i class="Hui-iconfont"></i> 下载模板表格</a>
//不能采用
<a href="javascript:;" onclick="projectreview_down('下载表格','/projectreview/downtemplate')" class="btn btn-primary radius"><i class="Hui-iconfont"></i> 下载模板表格</a>
</span>
//controller
@GetMapping(value = "/downtemplate")
@ResponseBody
public void downTemplate(HttpServletResponse response) {
InputStream inputStream=null;
try {
RestResult restResult=new RestResult();
restResult.setSuccess(false);
response.reset();
response.setContentType("bin");
response.setHeader("Content-Disposition", "attachment;filename=" + new String("销售项目评审表项目2019XXXX.xlsx".getBytes(), "ISO-8859-1"));
ServletOutputStream outputStream = response.getOutputStream();
inputStream= new FileInputStream(new File(ResourceUtils.getURL("classpath:").getPath()+"static/model/xs_table.xlsx"));
byte [] buff=new byte[1024];
int length=0;
while((length=inputStream.read(buff))!=-1){
outputStream.write(buff, 0, length);
}
if(outputStream!=null){
outputStream.flush();
outputStream.close();
}
}catch (Exception e){
e.printStackTrace();
}finally {
if(inputStream!=null){
try {
inputStream.close();
} catch (IOException e) {
log.error("关闭资源出错"+e.getMessage());
e.printStackTrace();
}
}
}
自己写的看到的,麻烦指点下。
https://blog.csdn.net/drsbbbl/article/details/105086908
https://blog.csdn.net/drsbbbl/article/details/103092726