Webkit JavaScript 可以将 REST 风格的图像作为纯二进制文件 PUT 或 POST 吗?
当我的浏览器从网站下载图像时,不涉及 Base64 编码。它发出一个类似 GET /image.jpg
的 HTTP 命令,并接收一个 Content-Type
类似 image/jpg
的 HTTP 响应,其 < code>Content-Length 是图像中的字节数,其主体是原始二进制图像数据本身。数据既不使用字符集进行编码,也不使用像 Base64 这样的方案进行编码。
编写 RESTful 资源训练我期望 HTTP GET
和 PUT
之间的对称性,例如,当我执行 GET
时,URL 会传递 JSON 数据然后,当使用 PUT
提供 JSON 数据时,code> 将接受 JSON 数据。在这两种情况下都不涉及形式编码;在这两种情况下,只有一个 Content-Length
给出有效负载中的字节数,一个 Content-Type
标头声明有效负载是某种字符集中的 JSON,以及那么字符数据就单独存在并且不加修饰地作为 GET
或 PUT
的主体。
我正在编写一个 PhoneGap 应用程序,允许用户拍照并使用我的应用程序上传照片。我原以为我可以为此设计一个 RESTful 接口,支持对称 GET
和 PUT
- 这样 PUT
命令不涉及特殊编码,也不涉及任何字符集的概念,而只是将 Content-Type
设为 image/jpg
,然后将大量二进制 JPG 数据作为其有效负载。显然,这比尝试在表单内对图像进行编码更能有效地利用带宽。当我使用 curl
等工具 PUT
到 URL 时,这种方法效果很好。
但我没有运气从 PhoneGap WebKit JavaScript 执行干净的 RESTful PUT
! PhoneGap 愿意将图像作为本地 file:
URL 或作为 data:
URL 返回到我的 JavaScript,该 URL 携带通过 base64 编码内联的图像数据。但在这两种情况下,我都找不到一种清晰的方法将图像转换为纯二进制格式(我会为此使用那些新奇的 Blob
对象之一吗?如果是的话,怎么做?),然后导致 < code>PUT ,无需使用额外的形式层或编码繁琐来装饰请求,只需将原始图像作为 HTTP 请求负载通过网络传输到我的 Web 服务器。
有谁知道如何诱导 WebKit PUT
一个以原始图像作为主体的 AJAX 请求?感谢您的任何指点 - 甚至任何有用的答案,大意是我处理这一切都是错误的!
When my browser downloads an image from a web site, there is no base64 encoding involved. It issues an HTTP command like GET /image.jpg
and receives an HTTP response whose Content-Type
is something like image/jpg
, whose Content-Length
is the number of bytes in the image, and whose body is the raw binary image data itself. The data is neither encoded using a character set, nor encoded using a scheme like base64.
Writing RESTful resources has trained me to expect symmetry between HTTP GET
and PUT
so that, for example, a URL that delivers JSON data when I do a GET
will then accept JSON data when it is presented with a PUT
. In neither case is form encoding involved; in both cases there is simply a Content-Length
giving the number of bytes in the payload, a Content-Type
header declaring that the payload is JSON in some character set, and then the character data sits alone and unadorned as the body of the GET
or PUT
.
I am writing a PhoneGap application that lets users take photographs and them upload them using my application. I had expected that I could design a RESTful interface for this, that supports symmetric GET
and PUT
— so that PUT
commands involve no special encoding, nor involve any idea of a character set, but simply have a Content-Type
of image/jpg
and then a mass of binary JPG data as their payload. Obviously this is a more efficient use of bandwidth than trying to encode the image inside of a form. And this approach works fine when I PUT
to a URL using a tool like curl
.
But I am having no luck in doing a clean RESTful PUT
from PhoneGap WebKit JavaScript! PhoneGap is willing to return the image to my JavaScript as either a local file:
URL, or else as a data:
URL that carries the image data inline courtesy of base64 encoding. But in neither case can I find a clear way to convert the image into a pure binary format (would I use one of those newfangled Blob
objects for this? if so, how?) and then cause a PUT
that, without festooning the request with extra layers of form or encoding cruft, will simply transmit the raw image across the wire to my web server as the HTTP request payload.
Does anyone know how to induce WebKit to PUT
an AJAX request with a raw image as its body? Thanks for any pointers — or even any useful answers to the effect that I am approaching this all wrong!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
至于二进制图像处理,二进制文件检查器演示可能会有所帮助-到结束的例子。
MDNBlob 主题的教程信息一个>。 这篇博文介绍了如何将 base64 URI 转换为
ArrayBuffer
,然后转换为Blob
。最后,可以上传
ArrayBuffer
或Blob
使用XMLHttpRequest2
接口。XHR2 规范似乎暗示支持
PUT
以及许多其他HTTP方法。因此,结合所有这些,您可以将文件填充到 Blob 中并使用 XHR2 发送它们。使用 base64 解码方法的未经测试的代码示例,并得到 base64 的帮助-binary.js:
也就是说,我怀疑其中大部分内容是否能在最新的桌面 WebKit 上运行,更不用说您将通过 PhoneGap 获得的版本了。您可能不适合利用这些草案规范。
As for the binary image processing, the Binary File Inspector demo might be helpful as an end-to-end example.
There's some tutorial information on the subject of getting
Blob
s from the filesystem at MDN. This blog post describes how to turn a base64 URI into anArrayBuffer
and then aBlob
.Finally, either an
ArrayBuffer
or aBlob
can be uploaded using theXMLHttpRequest2
interface.The XHR2 spec seems to imply that
PUT
is supported, along with a bunch of other HTTP methods. So combining all this you could stuff files inBlob
s and send them up using XHR2.Untested code sample using the base64 decoding method with some help from base64-binary.js:
That said, I doubt much of this works on even the latest desktop WebKit, much less the version you're going to be saddled with via PhoneGap. You're probably not in good shape to take advantage of these draft specifications.