文件上传API:主体中的多部分/表单数据与原始内容? [英] File upload API: Multipart/form-data vs. raw contents in body?

查看:84
本文介绍了文件上传API:主体中的多部分/表单数据与原始内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到(至少)有两种通过API将文件上传到HTTP服务器的方式.

您可以使用multipart/form-data(这是浏览器在本地执行的用于文件上传HTML输入的操作),但是您也可以POST请求正文中的文件内容(也许具有正确的Content-Type请求标头). /p>

每种方法的优缺点是什么(总的来说,不是来自浏览器)?

例如,多部分请求–根据您在编程环境中使用的http或网络库(我在服务器端使用Node.js,在客户端使用Swift)–创建然后再复杂一些解析.

解决方案

协议级别的唯一区别是multipart/form-data请求必须遵守 解决方案

The only difference on the protocol level is that multipart/form-data requests must adhere to RFC 2388 while a custom typed request body can be arbitrary.

The practical implication from this is that a multipart/form-data request is typically larger: While clients are technically allowed to use a non-7bit content-transfer-encoding, base64 is used by most. The MIME headers generate additional overhead that can become a bottleneck if many small files are uploaded. Note that support for multipart/form-data file uploads in existing clients/libraries is far more widespread. You should always provide it as a fallback if you are not sufficiently certain about the featureset of your clients and intermediate hosts (proxy servers). Especially keep in mind that if you are designing an API for third parties that other developers will already be familiar with multipart/form-data and have libraries at hand to work with that.

这篇关于文件上传API:主体中的多部分/表单数据与原始内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆