我们可以将二进制原始数据添加到< form>吗? [英] Can we add binary raw data to a <form>?

查看:48
本文介绍了我们可以将二进制原始数据添加到< form>吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将二进制原始数据添加到表单中,例如在此 data 字段中:

Is it possible to add binary raw data to a form, e.g. in this data field:

<form action="/upload" method="post">
<input type="hidden" id="data" />
<input type="submit" />
</form>

?

在我的用例中,我想使用

In my use case, I want to export an image from a canvas to JPEG with toDataURL, then decode the base64 to binary data, add the binary data to the <form>, which will be ready for submitting.

为什么我仅发布base64编码的数据?因为我想节省客户端上载时间/服务器上载带宽的1/3,所以众所周知,base64比二进制数据大1.333倍.

Why do I want to not just post the base64-encoded data? Because I would like to save 1/3 of uploading time for client / bandwidth for server, and it is well known base64 is bigger than binary data of a factor 1.333.

推荐答案

如果这是您的表单

<form action="something.php"  method="post" enctype="multipart/form-data">
  <input type="text" name="firstname" value="Peter">
  <input type="text" name="lastname" value="Quill">
  <input type="file" name="photo">
  <input type="submit" value="Submit">
</form> 

这是发送方式

------WebKitFormBoundarybHHp9cSVfgrymPhN
Content-Disposition: form-data; name="firstname"

Peter
------WebKitFormBoundarybHHp9cSVfgrymPhN
Content-Disposition: form-data; name="lastname"

Quill
------WebKitFormBoundarybHHp9cSVfgrymPhN
Content-Disposition: form-data; name="photo"; filename=""
Content-Type: application/octet-stream


------WebKitFormBoundarybHHp9cSVfgrymPhN--

每个表单字段的值都被包围,浏览器会智能地计算边界,因为它会遍历整个表单文本框,文本区域等(包括文件输入的原始文件字节流)所有这些都是为了避免分隔符冲突.浏览器将在边界之前加上各自的供应商像webkit这样的前缀,但最终它始终是可靠的定界符.

Every form field's value is enveloped the boundary is calculated intelligently by the browser for that it goes over the entire form textbox, textareas etc (including raw file bytestream of the file inputs) all this in order to avoid Delimiter collision.Browsers will prefix the boundary with their respective vendor prefix like webkit here but in the end it always be a reliable delimiter.

现在,即使您可以进行这种低级的数据组装,但是如果碰巧需要输入文件,创建一个真正可靠的定界符将是一项艰巨的任务.这就是为什么您应该让浏览器来处理建议的事情基于Formdata的解决方案与此配合得很好.

Now even if you get your hands on this low level assembling of data, creating a really reliable delimiter will be a hefty task if you happen to have file inputs to worry about.That's why you should let browser handle such stuff the suggested Formdata based solution goes well with this.

enctype ='multipart/form-data'是发送数据的唯一机制,无论其性质如何,所有其他enctype( application/x-www-form-urlencoded text/plain )仅支持ASCII传输. entype (s)本质上是一种告诉浏览器应使用哪种方案来提交表单的方式,但该方案本身不是仅限于纯粹的形式,因为您可以看到使用相同形式的Formdata.

enctype='multipart/form-data' is the only mechanism to send data regardless of it's nature all other enctype (application/x-www-form-urlencoded,text/plain) just support ASCII transfer.entype(s) are essentially a way of telling which scheme the browser should use to submit a form but the scheme itself isn't limited to mere forms as you can see Formdata using the same.

这篇关于我们可以将二进制原始数据添加到&lt; form&gt;吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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