写文件回前端进行下载,报错:原因:CORS 头缺少 ‘Access-Control-Allow-Origin‘)

后端写文件返回前端,出现该错误。

解决

设置允许跨域

response.setHeader("Access-Control-Allow-Origin", "*");

代码

后端

public void exportTemplate(HttpServletResponse response) {
ArrayList<ActiveGifts> activeGifts = new ArrayList<>();
String fileName = DateUtil.format(new Date(), "yyyyMMddHHmmss") + "模板文件.xlsx";

// 对文件名进行URI编码
try {
// 清空response
response.reset();
response.setCharacterEncoding("UTF-8");
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-disposition", "attachment;filename=" + new String(fileName.getBytes(StandardCharsets.UTF_8), "ISO8859-1") + ";filename*=utf-8''" + URLEncoder.encode(fileName, StandardCharsets.UTF_8));
//允许跨域
response.setHeader("Access-Control-Allow-Origin", "*");
// 用 EasyExcel 写入响应输出流
EasyExcel.write(response.getOutputStream(), ActiveGifts.class).sheet("模板").doWrite(activeGifts);
} catch (IOException e) {
e.printStackTrace();
}
}

前端

activeGiftsApi.activeGiftsDownloadTemp().then((res) => {
   let blob = new Blob([res.data], {type: 'application/vnd.ms-excel;charset=utf-8'}) // 文件类型
   console.log(res.headers['content-disposition']); // 从response的headers中获取filename, 后端response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx") 设置的文件名;
   //以=分割取数组[1]元素为文件名
   let filename = '123123'
   let url = window.URL.createObjectURL(blob);  // 创建下载链接
   let aLink = document.createElement("a");    // 赋值给a标签的href属性
   aLink.style.display = "none";
   aLink.href = url;
   aLink.setAttribute("download", filename);
   document.body.appendChild(aLink);   // 将a标签挂载上去
   aLink.click();          // a标签click事件
   document.body.removeChild(aLink);  // 移除a标签
   window.URL.revokeObjectURL(url);   // 销毁下载链接
})
此插为跨域插,打开后可跨域访问接口,旧版浏览器可直接拖到扩展程序中安装,新版浏览器需要将扩展名修改成rar并解压,然后点击“加载已解压的扩展程序”安装。 ======================插概述谷歌译文====================== 轻松将(Access-Control-Allow-Origin:*)规则添加到响应标。 允许CORS:通过Access-Control-Allow-Origin,您可以轻松地在Web应用程序中执行跨域Ajax请求。 只需激活插并执行请求。默认情况下(在JavaScript API中),CORS或跨源资源共享在现代浏览器中被阻止。安装此加载项将使您可以解除阻止此功能。请注意,将插添加到浏览器后,默认情况下它处于非活动状态(工具栏图标为灰色C字母)。如果要激活加载项,请按一次工具栏图标。图标将变为橙色的C字母。 ======================插概述谷歌译文====================== ========================插概述原文======================== Easily add (Access-Control-Allow-Origin: *) rule to the response header. Allow CORS: Access-Control-Allow-Origin lets you easily perform cross-domain Ajax requests in web applications. Simply activate the add-on and perform the request. CORS or Cross Origin Resource Sharing is blocked in modern browsers by default (in JavaScript APIs). Installing this add-on will allow you to unblock this feature. Please note that, when the add-on is added to your browser, it is in-active by default (toolbar icon is grey C letter). If you want to activate the add-on, please press on the toolbar icon once. The icon will turn to orange C letter. ========================插概述原文========================
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值