通过window.open下载pdf或直接打开文件

通过window.open下载pdf或直接打开文件

后端设置

@RestController
@RequestMapping("/print")
public class PrintController {
	@GetMapping("/out")
	public void out(PrintVo vo,  HttpServletResponse response) throws IOException {
		response.addHeader("content-disposition", "inline;filename=file.pdf");
		response.setContentType("application/pdf");
		//  这里生成pdf 并写入了 response.getOutputStream()
		PrintOutUtil.generatePdf(data, modelPath, outFilePath, response.getOutputStream());
		response.getOutputStream().flush();
		response.flushBuffer();
	}
}

前端设置

window.open(window.origin + '/dev-api/print/out','_blank');

总结

1、content-disposition 设置
content-disposition的属性设置成inline,代表在浏览器中打开文件。
2、ContentType 设置
ContentType 需要设置成application/pdf,代表文件类型为pdf。
3、如果是在新窗口直接下载,content-disposition的属性设置成attachment。

response.addHeader("content-disposition", "attachment;filename=file.pdf");
  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值