我有一个简单的 Spring MVC Controller 方法来返回 PDF:
@RequestMapping(value="/attachment/portfolios/{investorNum}/reports/{reportId}/periods/{reportingPeriod}")
public ResponseEntity<byte[]> getReportForDownload(@PathVariable String reportId, @PathVariable long investorNum, @PathVariable long reportingPeriod) throws IOException{
InputStream in = servletContext.getResourceAsStream("/sample.pdf");
final HttpHeaders headers = new HttpHeaders();
//headers.setContentType(MediaType.valueOf("application/pdf"));
headers.add("Content-Type", "application/pdf");
headers.add("Content-disposition", "attachment; filename=abcd.pdf");
return new ResponseEntity<byte[]>(IOUtils.toByteArray(in), headers, HttpStatus.CREATED);
}
还有一个 spring bytearray 消息转换器:
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<util:list>
<bean id="byteArrayMessageConverter" class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/>
</util:list>
</property>
</bean>
还有一个可供下载的 jQuery 模式框:
"Download" : function() {
location.href = "./attachment/portfolios/2201/reports/DELINQSUM/periods/1137";
}
下载按钮在 FF、Chrome 中工作得非常好,但在 IE 中却不行。 IE 说
Internet Explorer cannot download 1137 from localhost.
block 引用>Internet Explorer was not able to open this Internet site. The requested site is either unavailable or cannot be found. Please try again later.
我按照 http://support.microsoft.com/kb/316431 中的标题进行操作和 2但没有任何反应,IE 继续报告该问题。
另外,我正在流式传输 PDF 以在模态中嵌入显示,并进行以下更改,效果很好:
headers.add("Content-disposition", "inline; filename=abcd.pdf");
我在 tomcat 上运行它。
非常感谢任何帮助。
最佳答案
我也遇到过同样的问题。我正在使用 Apache FOP 生成 PDF,并使用与您拥有的相同 header 和 ResponseEntity
以 Spring 方式将其写入响应。
我通过在 ResponseEntity
构造函数中选择 HttpStatus.OK
而不是 HttpStatus.CREATED
解决了该问题。
显然 IE8 无法将 201 (CREATED) 理解为响应代码。
关于jquery - 用于 PDF 下载、location.href、jQuery Modal 和 IE8 header 的 Spring Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10547432/