Content-disposition中Attachment和inline的区别

简介:

Java Web中下载文件时,我们一般设置Content-Disposition告诉浏览器下载文件的名称,是否在浏览器中内嵌显示.

Content-disposition: inline; filename=foobar.pdf

表示浏览器内嵌显示一个文件

 

Content-disposition: attachment; filename=foobar.pdf

表示会下载文件,如火狐浏览器中

 

spring mvc中

Java代码   收藏代码
  1. @ResponseBody  
  2.     @RequestMapping(value = "/download",produces="application/octet-stream")  
  3.     public byte[] downloadFile(HttpServletRequest request, HttpServletResponse response,String contentType2)  
  4.             throws IOException {  
  5.         byte[]bytes=FileUtils.getBytes4File("D:\\Temp\\cc.jpg");  
  6.         response.addHeader("Content-Disposition""inline;filename=\"a.jpg\"");  
  7.         return bytes;  
  8.     }  

 如上代码中是内嵌显示图片呢?还是会弹框下载呢?

答案是:弹框下载

为什么呢?设置为inline应该是内嵌显示啊!

因为response content type设置成了"application/octet-stream"

注意:我们说是内嵌显示还是下载,那一定是针对可内嵌显示的类型,例如"image/jpeg","image/png"等.

 

看下面的例子:设置response content type为"image/jpeg"

Java代码   收藏代码
  1. @ResponseBody  
  2.     @RequestMapping(value = "/download",produces="image/jpeg")  
  3.     public byte[] downloadFile(HttpServletRequest request, HttpServletResponse response,String contentType2,String downloadType)  
  4.             throws IOException {  
  5.         byte[]bytes=FileUtils.getBytes4File("D:\\Temp\\cc.jpg");  
  6.         response.addHeader("Content-Disposition", downloadType+";filename=\"a.jpg\"");  
  7.         return bytes;  
  8.   
  9.     }  

 在浏览器中访问:http://localhost:8080/tv_mobile/video/download?downloadType=inline 时就内嵌显示:



 

当在浏览器中访问:http://localhost:8080/tv_mobile/video/download?downloadType=attachment  时就弹框下载.

参考:http://hw1287789687.iteye.com/blog/2188480

相关文章
|
4月前
|
JSON Java 数据格式
Could not extract response: no suitable HttpMessageConverter found for ..content type [text/html...]
Could not extract response: no suitable HttpMessageConverter found for ..content type [text/html...]
103 0
|
6月前
head 插件 Content-Type header [application/x-www-form-urlencoded] is not supported
head 插件 Content-Type header [application/x-www-form-urlencoded] is not supported
46 1
|
10月前
|
JavaScript 前端开发 应用服务中间件
【已解决】“X-Content-Type-Options”头缺失或不安全
【已解决】“X-Content-Type-Options”头缺失或不安全
787 0
|
11月前
|
Java 测试技术
body-parser:unsupported content encoding 错误
最近遇到了一个奇怪的问题,关于body-parser报错,我本地调用没问题,使用测试工具没问题,这种方案都没问题,就和我对接的程序调用有问题,于是开始了面向百度编程,查到了两种解决方案:
404 0
body-parser:unsupported content encoding 错误
|
JavaScript 前端开发
JavaScript 无法获取响应 header 的 Content-Disposition 字段
JavaScript 无法获取响应 header 的 Content-Disposition 字段
829 0
JavaScript 无法获取响应 header 的 Content-Disposition 字段
modified: (modified content, untracked content)
modified: (modified content, untracked content)
modified: (modified content, untracked content)
|
XML JSON JavaScript
二、HTTP Content-Type详解
二、HTTP Content-Type详解
265 1
|
前端开发 对象存储 CDN
Hi gays, 你造Content-Disposition吗?
今天要和大家聊的就是导致这起线上问题的 Content-disposition 到底是何方神圣。
|
Web App开发
Inspect file content when uploading as attachment
Inspect file content when uploading as attachment
104 0
Inspect file content when uploading as attachment