diff options
author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
---|---|---|
committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
commit | 33058f2b292b3a581333bdfb21b8f671898c5060 (patch) | |
tree | 51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/http/headers/content-disposition | |
parent | 8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff) | |
download | translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2 translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip |
initial commit
Diffstat (limited to 'files/zh-cn/web/http/headers/content-disposition')
-rw-r--r-- | files/zh-cn/web/http/headers/content-disposition/index.html | 138 |
1 files changed, 138 insertions, 0 deletions
diff --git a/files/zh-cn/web/http/headers/content-disposition/index.html b/files/zh-cn/web/http/headers/content-disposition/index.html new file mode 100644 index 0000000000..abbd2dd7dc --- /dev/null +++ b/files/zh-cn/web/http/headers/content-disposition/index.html @@ -0,0 +1,138 @@ +--- +title: Content-Disposition +slug: Web/HTTP/Headers/Content-Disposition +tags: + - 上传 + - 文件下载 + - 表单 + - 首部 +translation_of: Web/HTTP/Headers/Content-Disposition +--- +<div>{{HTTPSidebar}}</div> + +<div>在常规的 HTTP 应答中,<code><strong>Content-Disposition</strong></code> 响应头指示回复的内容该以何种形式展示,是以<strong>内联</strong>的形式(即网页或者页面的一部分),还是以<strong>附件</strong>的形式下载并保存到本地。</div> + +<div></div> + +<p>在 multipart/form-data 类型的应答消息体中,<strong><code>Content-Disposition</code></strong> 消息头可以被用在 multipart 消息体的子部分中,用来给出其对应字段的相关信息。各个子部分由在{{HTTPHeader("Content-Type")}} 中定义的<strong>分隔符</strong>分隔。用在消息体自身则无实际意义。</p> + +<p>Content-Disposition 消息头最初是在 MIME 标准中定义的,HTTP 表单及 {{HTTPMethod("POST")}} 请求只用到了其所有参数的一个子集。只有 <code>form-data</code> 以及可选的 <code>name</code> 和 <code>filename</code> 三个参数可以应用在HTTP场景中。</p> + +<table class="properties"> + <tbody> + <tr> + <th scope="row">Header type</th> + <td>{{Glossary("Response header")}} (for the main body)<br> + {{Glossary("General header")}} (for a subpart of a multipart body)</td> + </tr> + <tr> + <th scope="row">{{Glossary("Forbidden header name")}}</th> + <td>no</td> + </tr> + </tbody> +</table> + +<h2 id="语法">语法</h2> + +<h3 id="作为消息主体中的消息头">作为消息主体中的消息头</h3> + +<p>在 HTTP 场景中,第一个参数或者是 <code>inline</code>(默认值,表示回复中的消息体会以页面的一部分或者整个页面的形式展示),或者是 <code>attachment</code>(意味着消息体应该被下载到本地;大多数浏览器会呈现一个“保存为”的对话框,将 <code>filename</code> 的值预填为下载后的文件名,假如它存在的话)。</p> + +<pre class="syntaxbox notranslate">Content-Disposition: inline +Content-Disposition: attachment +Content-Disposition: attachment; filename="filename.jpg"</pre> + +<h3 id="作为multipart_body中的消息头">作为multipart body中的消息头</h3> + +<p>在 HTTP 场景中。第一个参数总是固定不变的 <code>form-data</code>;附加的参数不区分大小写,并且拥有参数值,参数名与参数值用等号(<code>'='</code>)连接,参数值用双引号括起来。参数之间用分号(<code>';'</code>)分隔。</p> + +<pre class="syntaxbox notranslate">Content-Disposition: form-data +Content-Disposition: form-data; name="fieldName" +Content-Disposition: form-data; name="fieldName"; filename="filename.jpg"</pre> + +<h3 id="指令">指令</h3> + +<dl> + <dt><code>name</code></dt> + <dd>后面是一个表单字段名的字符串,每一个字段名会对应一个子部分。在同一个字段名对应多个文件的情况下(例如,带有 {{htmlattrxref("multiple", "input")}} 属性的 <code>{{HTMLElement("input","<input type=file>")}}</code> 元素),则多个子部分共用同一个字段名。如果 name 参数的值为 <code>'_charset_'</code> ,意味着这个子部分表示的不是一个 HTML 字段,而是在未明确指定字符集信息的情况下各部分使用的默认字符集。</dd> + <dt><code>filename</code></dt> + <dd>后面是要传送的文件的初始名称的字符串。这个参数总是可选的,而且不能盲目使用:路径信息必须舍掉,同时要进行一定的转换以符合服务器文件系统规则。这个参数主要用来提供展示性信息。当与 <code>Content-Disposition: attachment</code> 一同使用的时候,它被用作"保存为"对话框中呈现给用户的默认文件名。</dd> + <dt>filename*</dt> + <dd> + <p>"filename" 和 "filename*" 两个参数的唯一区别在于,"filename*" 采用了 <a href="https://tools.ietf.org/html/rfc5987">RFC 5987</a> 中规定的编码方式。当 "filename" 和 "filename*" 同时出现的时候,应该优先采用 "filename*",假如二者都支持的话。</p> + </dd> +</dl> + +<h2 id="示例">示例</h2> + +<p>以下是一则可以触发"保存为"对话框的服务器应答:</p> + +<pre class="notranslate">200 OK +Content-Type: text/html; charset=utf-8 +Content-Disposition: attachment; filename="cool.html" +Content-Length: 22 + +<HTML>Save me!</HTML> + +</pre> + +<p>这个简单的 HTML 文件会被下载到本地而不是在浏览器中展示。大多数浏览器默认会建议将 <code>cool.html</code> 作为文件名。</p> + +<p>以下是一个HTML表单的示例,展示了在 <code>multipart/form-data</code> 格式的报文中使用<code>Content-Disposition</code> 消息头的情况:</p> + +<pre class="notranslate">POST /test.html HTTP/1.1 +Host: example.org +Content-Type: multipart/form-data;boundary="boundary" + +--boundary +Content-Disposition: form-data; name="field1" + +value1 +--boundary +Content-Disposition: form-data; name="field2"; filename="example.txt" + +value2 +--boundary--</pre> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + <th scope="col">Title</th> + </tr> + <tr> + <td>{{RFC("7578")}}</td> + <td>Returning Values from Forms: multipart/form-data</td> + </tr> + <tr> + <td>{{RFC("6266")}}</td> + <td>Use of the Content-Disposition Header Field in the Hypertext Transfer Protocol (HTTP)</td> + </tr> + <tr> + <td>{{RFC("2183")}}</td> + <td>Communicating Presentation Information in Internet Messages: The Content-Disposition Header Field</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<p class="hidden">The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</p> + +<p>{{Compat("http.headers.Content-Disposition")}}</p> + +<h2 id="兼容性说明">兼容性说明</h2> + +<ul> + <li>在 <code>filename</code> 和 <code>filename*</code> 两个参数同时出现的情况下,Firefox 5 (比以前的版本)可以更好地处理 <code>Content-Disposition</code> 应答消息头。它会遍历所有提供的名称,假如 <code>filename*</code> 存在的话,就采用它的值,即使 <code>filename</code> 更靠前。之前的版本会采用出现在前面的参数的值,导致有更合适的名称而不被使用。参见{{bug(588781)}}.</li> +</ul> + +<h2 id="相关链接">相关链接</h2> + +<ul> + <li><a href="/en-US/docs/Web/Guide/HTML/Forms">HTML Forms</a></li> + <li>{{HTTPHeader("Content-Type")}} 中定义了 multipart body 的分隔符</li> + <li>{{domxref("FormData")}} 接口可以用来操作在 {{domxref("XMLHttpRequest")}} API中使用的表单数据。</li> +</ul> |