【发布时间】:2020-09-22 15:15:18
【问题描述】:
我必须手动为多部分/表单数据 POST 请求构建正文。我理解结构很好,我可以成功上传不包含文件的表单。我有一个文件作为File 对象,我需要将文件的内容解释为字符串以将它们包含在请求的正文中。我遇到的所有带有文件的多部分表单数据的示例都只有类似 "contents of file go here" 的内容,其中包含文件,并且从不讨论如何从文件获取到字符串。 this 问题的最佳答案接近我正在寻找的内容,但我更愿意避免 base64 的额外开销,因为我的表单将处理许多文件。我发现了
`
--${boundary}
Content-Disposition: form-data; name="file"; filename="${file.name}"
Content-Type: ${file.type}
${await file.text()}`
适用于简单的 pdf,但使用 jpeg 失败(这里的“失败”意味着我的服务器无法正确解析图像)。
我有一个使用带有 Fetch 的 FormData 实例的工作示例(我不能在生产中使用 FormData)。在 Chrome 开发人员工具中,我可以获取请求的原始正文以查看文件的外观。以下是文件开头的样子:
Content-Disposition: form-data; name="file"; filename="test.jpg"
Content-Type: image/jpeg
ÿØÿî!AdobedÀ E¿d„¾¤ÿÛ„
$$''$$53335;;;;;;;;;;
使用file.text() 消息的相同部分如下所示:
����!Adobed� E�d������
$$''$$5333
当文件被这样解码时:
`
--${boundary}
Content-Disposition: form-data; name="file"; filename="${file.name}"
Content-Type: ${file.type}
${String.fromCharCode.apply(null, new Uint8Array(await file.arrayBuffer()))}`
}
result += `
文件的开头看起来是正确的,但比较完整的字符串表明存在一些差异。
我找到了这个
4.3 Encoding
While the HTTP protocol can transport arbitrary binary data, the
default for mail transport is the 7BIT encoding. The value supplied
for a part may need to be encoded and the "content-transfer-encoding"
header supplied if the value does not conform to the default
encoding. [See section 5 of RFC 2046 for more details.]
在 RFC 2388 中,但我相信这是指请求正文是如何通过网络发送的,而不是关于正文是如何构造的。我觉得我在这里缺少一些核心概念。任何帮助将不胜感激。
编辑: 以下是表单数据发送到我的服务器的方式:
const response = await fetch(url, {
method: 'POST', // *GET, POST, PUT, DELETE, etc.
mode: 'cors', // no-cors, *cors, same-origin
cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
credentials: 'same-origin', // include, *same-origin, omit
redirect: 'follow', // manual, *follow, error
referrer: 'no-referrer', // no-referrer, *client
body: serializedData, // body data type must match "Content-Type" header
headers: {
'Content-Type': 'multipart/form-data; boundary=' + boundary,
},
})
【问题讨论】:
-
您是如何提交表单的?你说
I cannot use FormData in production——那么你可以在生产中使用什么?为什么不能使用 FormData? -
我必须支持 IE 11,我的环境中有一些东西超出了我的控制范围,导致 polyfill 行为不端。我正在从表单中检索信息并自己打包。
-
对于电子邮件的多部分表单,我将图像保存到一个目录,然后使用电子邮件正文中的完整路径从该目录引用文件。
-
感谢@SJacks,但我在浏览器环境中工作。
标签: javascript multipartform-data