边界在多部分发布请求中如何工作?

发布于 2024-09-28 19:51:43 字数 108 浏览 0 评论 0原文

我尝试将文件从 iPhone 上传到服务器。我试图避免使用任何非苹果制作的库,据我所知,我需要在构建我的请求时达到相当低的水平。有人可以告诉我多部分/表单数据请求中的“边界”是什么以及如何正确使用它吗?

I trying to upload a file from an iPhone to a server. I'm trying to avoid using any libraries that aren't made by apple, and from what I can tell it looks like I'll need to go pretty low level on constructing my request. Can someone tell me what the "boundary" is in a multipart/form-data request and how to use it properly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

睫毛溺水了 2024-10-05 19:51:43

边界是任意一段文本,客户端使用它来分隔所发布的表单的字段。客户端声明其使用的边界作为 Content-type 标头的一部分。

来自 IETF HTML 中基于表单的文件上传 RFC

选择的边界不会出现在任何数据中。 (这
选择有时是概率性地完成的。)表单的每个字段
被发送,按照它在表单中出现的顺序,作为
多部分流。每个部分都标识了 INPUT 名称
原始 HTML 形式。每个部分都应贴有适当的标签
content-type 如果媒体类型已知(例如,从文件推断)
扩展名或操作系统键入信息)或作为
应用程序/八位字节流。

...

6.示例

假设服务器提供以下 HTML:

你叫什么名字? <输入类型=文本名称=提交者> 您要发送什么文件? <输入类型=文件名=图片>

用户在名称字段中键入“Joe Blow”,然后选择一个文本
文件“file1.txt”作为“您要发送哪些文件?”的答案

客户端可能会发回以下数据:

 内容类型:multipart/form-data,边界=AaB03x

   --AaB03x
   内容处置:形式数据;名称=“字段1”

   乔·布洛
   --AaB03x
   内容处置:形式数据;名称=“图片”;文件名=“文件1.txt”
   内容类型:文本/纯文本

    ... file1.txt 的内容 ...
   --AaB03x--

如果用户还指定了图像文件“file2.gif”作为答案
到“您要发送什么文件?”,客户端可能会发送
返回以下数据:

 内容类型:multipart/form-data,边界=AaB03x

   --AaB03x
   内容处置:形式数据;名称=“字段1”

   乔·布洛
   --AaB03x
   内容处置:形式数据;名称=“图片”
   内容类型:多部分/混合,边界=BbC04y

   --BbC04y
   内容处置:附件;文件名=“文件1.txt”

在第一个示例中,边界是固定字符串AaB03x。在第二个示例中,边界首先是 AaB03x,然后切换到 BbC04y

The boundary is an arbitrary piece of text which the client uses to delimit the fields of the form being posted. The client declares the boundary it is using as part of the Content-type header.

From the IETF Form-based File Upload in HTML RFC:

A boundary is selected that does not occur in any of the data. (This
selection is sometimes done probabilisticly.) Each field of the form
is sent, in the order in which it occurs in the form, as a part of
the multipart stream. Each part identifies the INPUT name within the
original HTML form. Each part should be labelled with an appropriate
content-type if the media type is known (e.g., inferred from the file
extension or operating system typing information) or as
application/octet-stream.

...

6. Examples

Suppose the server supplies the following HTML:

<FORM ACTION="http://server.dom/cgi/handle"
       ENCTYPE="multipart/form-data"
       METHOD=POST>
 What is your name? <INPUT TYPE=TEXT NAME=submitter>
 What files are you sending? <INPUT TYPE=FILE NAME=pics>
 </FORM>

and the user types "Joe Blow" in the name field, and selects a text
file "file1.txt" for the answer to 'What files are you sending?'

The client might send back the following data:

   Content-type: multipart/form-data, boundary=AaB03x

   --AaB03x
   content-disposition: form-data; name="field1"

   Joe Blow
   --AaB03x
   content-disposition: form-data; name="pics"; filename="file1.txt"
   Content-Type: text/plain

    ... contents of file1.txt ...
   --AaB03x--

If the user also indicated an image file "file2.gif" for the answer
to 'What files are you sending?', the client might client might send
back the following data:

   Content-type: multipart/form-data, boundary=AaB03x

   --AaB03x
   content-disposition: form-data; name="field1"

   Joe Blow
   --AaB03x
   content-disposition: form-data; name="pics"
   Content-type: multipart/mixed, boundary=BbC04y

   --BbC04y
   Content-disposition: attachment; filename="file1.txt"

In the first example, the boundary is the fixed string AaB03x. In the second example, the boundary is first AaB03x and then switches to BbC04y.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文