Content-disposition中Attachment和inline的区别

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

Content-disposition: inline; filename=foobar.pdf

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

 

Content-disposition: attachment; filename=foobar.pdf

表示会下载文件,如火狐浏览器中
Content-disposition中Attachment和inline的区别_第1张图片
 

spring mvc中

@ResponseBody
	@RequestMapping(value = "/download",produces="application/octet-stream")
	public byte[] downloadFile(HttpServletRequest request, HttpServletResponse response,String contentType2)
			throws IOException {
		byte[]bytes=FileUtils.getBytes4File("D:\\Temp\\cc.jpg");
		response.addHeader("Content-Disposition", "inline;filename=\"a.jpg\"");
		return bytes;
	}

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

答案是:弹框下载

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

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

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

 

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

@ResponseBody
	@RequestMapping(value = "/download",produces="image/jpeg")
	public byte[] downloadFile(HttpServletRequest request, HttpServletResponse response,String contentType2,String downloadType)
			throws IOException {
		byte[]bytes=FileUtils.getBytes4File("D:\\Temp\\cc.jpg");
		response.addHeader("Content-Disposition", downloadType+";filename=\"a.jpg\"");
		return bytes;

	}

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


Content-disposition中Attachment和inline的区别_第2张图片
 

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

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



已有 0 人发表留言,猛击->> 这里<<-参与讨论


ITeye推荐
  • —软件人才免语言低担保 赴美带薪读研!—



你可能感兴趣的:(Attachment,content,disposition)