jquery - 用于 PDF 下载、location.href、jQuery Modal 和 IE8 header 的 Spring Controller

标签 jquery spring internet-explorer pdf download

我有一个简单的 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.

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/

相关文章:

javascript - 如果数字为 1 或 0,则显示不同的图标 - ASP.NET C# MVC JQuery

spring - thymeleaf 秒 :authorize not working in spring boot

javascript - 处理 IE 中的 JQuery/CSS 错误

javascript - jquery.post() 后端响应,但回调函数参数为空

javascript - jQuery 将隐藏元素值设置为 post() 数据

javascript - W3Schools 示例不起作用?

java - Spring Webflux webclient 出现问题,尝试发送 post 请求时没有任何反应

model-view-controller - javax.validation.ConstraintViolationException : Bean Validation constraint(s) violated on preUpdate validation

javascript - Internet Explorer 不会读取对象属性

asp.net-mvc - 当我的表单方法是 POST 时,用户请求 GET