LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

为什么浏览器中有些图片、PDF等文件点击后有些是预览,有些是下载

freeflydom
2024年7月24日 16:7 本文热度 314
为什么浏览器中有些图片、PDF等文件点击后有些是预览,有些是下载,
出现图片、PDF不能预览的情况,主要是通过代码输出文件流的形式,非IIS、Nginx下直接访问资源的方式产生的


在HTTP中,用于文件下载的常见响应头(response headers)包括:
Content-Disposition: 用于指示浏览器应该如何处理响应体。在文件下载中,通常设置为 attachment; filename="filename.jpg",其中 filename.jpg 是要下载的文件名。

Content-Type: 指示响应体的MIME类型,例如 image/jpeg 或 application/pdf。

Content-Length: 响应体的长度(以字节为单位)。

Cache-Control: 控制缓存的行为,通常可以设置为 no-cache 禁止缓存。

Expires: 指定响应的过期时间,与 Cache-Control 一起使用来控制缓存。

Pragma: 旧版本的HTTP使用的头部,现在基本不再使用,可以设置为 no-cache。

Content-Disposition

Content-Disposition 是一个HTTP响应头部,指示浏览器如何处理接收到的响应体内容。它可以让服务器指定一个文件名,或者指示浏览器直接显示内容(inline)或者将内容作为附件下载(attachment)。

  • inline:浏览器尝试在页面内显示响应内容,通常用于非下载内容,如 PDF 在线预览。

  • attachment:浏览器强制下载响应内容,并提示用户保存到本地。

Content-Disposition: inline

Content-Disposition: attachment

Content-Disposition: attachment; filename="example.txt"

文件下载

通常情况下,服务器会使用 Content-Disposition: attachment 头部来强制浏览器下载文件,而不是尝试在浏览器中打开它。例如:

Content-Disposition: attachment; filename="example.pdf"

Content-Type: application/pdf

内联显示

服务器可能希望浏览器直接在页面内显示内容,而不是下载它。例如,对于一些直接展示的文件类型(如图片、PDF等),可以使用

Content-Disposition: attachment; filename="example.pdf"

Content-Type: application/pdf

代码示例

SpringBoot 接口输出文件流:
https://www.cnblogs.com/vipsoft/p/16662762.html
https://www.cnblogs.com/vipsoft/p/16318030.html

//省去判断,参考下面的C#

headers.add("Content-Disposition", "attachment; filename=" + URLEncoder.encode(file.getName(), "UTF-8"));   //如果是 attachment 换成 inline ,像PDF,图片这样的就可以在浏览器内直接预览了

headers.add("filename",  URLEncoder.encode(file.getName(), "UTF-8"));

headers.add("Access-Control-Expose-Headers", "filename");  //这个要加,否则Vue 在 res.headers['filename'] 时取不到值

C# 伪代码

[HttpGet]

[Route("reportFile")]

public HttpResponseMessage ReportFile([FromBody] Order param)

{

HttpResponseMessage result = null;

try

{

    string projectId = HttpContext.Current.Request.QueryString["ProjectId"];

FileInfo foundFileInfo = new FileInfo(filePath);

if(!foundFileInfo.Exists)

  return new HttpResponseMessage(HttpStatusCode.NotFound);

}

string mediaType = "application/pdf";

string contentDisposition = "attachment"; 

//根据服务器文件的扩展名决定使用什么样的方式输出

switch (foundFileInfo.Extension)

{

case ".xml":

mediaType = "text/xml"; 

contentDisposition = "inline; //浏览器预览

break;

case ".pdf":

mediaType = "application/pdf";

contentDisposition = "inline; //浏览器预览

break;

case ".jpg":

case ".jpeg":

mediaType = "image/jpg";

contentDisposition = "inline; //浏览器预览

break;

case ".png":

mediaType = "image/png";

contentDisposition = "inline; //浏览器下载

break;

...

default:

mediaType = "application/octet-stream"; 

contentDisposition = "attachment"; //浏览器下载

break;

FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

result = new HttpResponseMessage(HttpStatusCode.OK);

result.Content = new StreamContent(fs);

//result.Content.Headers.Add("MD5File", Md5Helper.GetMD5HashFromFile(filePath)); //可以在Header里加上 md5 校验码

result.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(mediaType);

result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue(contentDisposition);

result.Content.Headers.ContentDisposition.FileName = foundFileInfo.Name;

}

catch (Exception ex)

{

logger.Error(ex, ex.Message);

result = new HttpResponseMessage(HttpStatusCode.BadRequest);

}

return result;

}

常见的媒体类型

  • text/plain:纯文本。

  • text/html:HTML 文档。

  • application/json:JSON 数据。

  • application/xml:XML 数据。

  • image/jpeg、image/png:JPEG 或 PNG 图像。

  • multipart/form-data:用于文件上传的表单数据。

文件上传

在使用表单上传文件时,通常会使用 multipart/form-data 媒体类型,并指定边界字符串。例如:

Content-Type: multipart/form-data; boundary=---------------------------7d33a816d302b6




该文章在 2024/7/24 16:07:53 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2024 ClickSun All Rights Reserved