Using jQuery#39;s ajax method to retrieve images as a blob(使用 jQuery 的 ajax 方法将图像作为 blob 检索)
问题描述
我最近问了另一个(相关)问题,这导致了这个后续问题:为输入表单提交数据而不是文件
I recently asked another (related) question, which lead to this follow up question: Submitting data instead of a file for an input form
阅读 jQuery.ajax() 文档 (http://api.jquery.com/jQuery.ajax/),似乎接受的数据类型列表不包括图像.
Reading through the jQuery.ajax() documentation (http://api.jquery.com/jQuery.ajax/), it seems the list of accepted dataTypes doesn't include images.
我正在尝试使用 jQuery.get(或 jQuery.ajax,如果我必须)检索图像,将此图像存储在 Blob 中,然后在 POST 请求中将其上传到另一台服务器.目前,看起来由于数据类型不匹配,我的图像最终被损坏(以字节为单位的大小不匹配等).
I'm trying to retrieve an image using jQuery.get (or jQuery.ajax if I have to), store this image in a Blob and upload it to another server in a POST request. Currently, it looks like due to the mismatch in datatypes, my images end up being corrupt (size in bytes mismatch, etc.).
执行此操作的代码如下(在coffeescript中但应该不难解析):
The code to perform this is as follows (it is in coffeescript but shouldn't be difficult to parse):
我怎样才能将此图像作为 blob 检索?
How can I retrieve this image as a blob instead?
推荐答案
你不能用 jQuery ajax 做这个,但是用原生的 XMLHttpRequest.
You can't do this with jQuery ajax, but with native XMLHttpRequest.
<小时>
编辑
所以重温这个话题,看来用 jQuery 3 确实可以做到这一点
So revisiting this topic, it seems it is indeed possible to do this with jQuery 3
或
使用 xhrFields 设置 responseType
use xhrFields to set the responseType
这篇关于使用 jQuery 的 ajax 方法将图像作为 blob 检索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!