HTTP中的Content-Disposition消息头


本文作者: jsweibo

本文链接: https://jsweibo.github.io/2020/02/28/HTTP%E4%B8%AD%E7%9A%84Content-Disposition%E6%B6%88%E6%81%AF%E5%A4%B4/

摘要

本文主要讲述了:

  1. 请求头
  2. 响应头

正文

请求头

在多资源请求体中,用于表示部分请求体的编码方式及表单中控件的name属性

示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<form method="POST" enctype="multipart/form-data">
<input type="text" name="username" value="jsweibo" />
<input type="file" name="avatar" />
<button>Submit</button>
</form>
</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
POST /index.html HTTP/1.1
Host: 127.0.0.1:3000
Proxy-Connection: keep-alive
Content-Length: 404
Cache-Control: max-age=0
Origin: http://127.0.0.1:3000
Upgrade-Insecure-Requests: 1
DNT: 1
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryLNIyFWTdmRKe5NfA
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36
Sec-Fetch-Dest: document
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Sec-Fetch-Site: same-origin
Sec-Fetch-Mode: navigate
Sec-Fetch-User: ?1
Referer: http://127.0.0.1:3000/index.html
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9

------WebKitFormBoundaryLNIyFWTdmRKe5NfA
Content-Disposition: form-data; name="username"

jsweibo
------WebKitFormBoundaryLNIyFWTdmRKe5NfA
Content-Disposition: form-data; name="avatar"; filename="c8fb3b2a99b3678c38b537af439b6ed5a13b0d73.png"
Content-Type: image/png

iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAA1JREFUCB1j+M/A8B8ABQAB/8gPYhYAAAAASUVORK5CYII=
------WebKitFormBoundaryLNIyFWTdmRKe5NfA--

注:出于可读性考虑,本示例中的iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAA1JREFUCB1j+M/A8B8ABQAB/8gPYhYAAAAASUVORK5CYII=其实是进行 Base64 编码之后的结果。原数据为十六进制数据。

响应头

表示资源的展现形式

取值范围:

  • inline
  • attachment

默认值为inline

inline

示例:内联(在浏览器内展示)

1
Content-Disposition: inline

注意:Content-Disposition: inline对于不支持在浏览器内展示的文件(例如:.7z)无效,对于这些文件,浏览器会自动触发另存为对话框

attachment

示例:附件(会触发浏览器的另存为对话框)

1
Content-Disposition: attachment

Content-Disposition响应头的值为attachment时,可以使用filename指令设置另存为对话框中文件的默认名称

示例:

1
Content-Disposition: attachment; filename=foo.jpg

参考资料

本文作者: jsweibo

本文链接: https://jsweibo.github.io/2020/02/28/HTTP%E4%B8%AD%E7%9A%84Content-Disposition%E6%B6%88%E6%81%AF%E5%A4%B4/


本文对你有帮助?请支持我


支付宝
微信