【问题标题】:Using jQuery's ajax method to retrieve images as a blob使用 jQuery 的 ajax 方法将图像作为 blob 检索
【发布时间】:2023-04-02 17:02:01
【问题描述】:

我最近问了另一个(相关)问题,这导致了这个后续问题: Submitting data instead of a file for an input form

阅读 jQuery.ajax() 文档 (http://api.jquery.com/jQuery.ajax/),似乎接受的数据类型列表不包括图像。

我正在尝试使用 jQuery.get(或 jQuery.ajax,如果我必须)检索图像,将此图像存储在 Blob 中,然后通过 POST 请求将其上传到另一台服务器。目前,看起来由于数据类型不匹配,我的图像最终被损坏(以字节为单位的大小不匹配等)。

执行此操作的代码如下(在coffeescript中但应该不难解析):

handler = (data,status) ->
  fd = new FormData
  fd.append("file", new Blob([data], { "type" : "image/png" }))
  jQuery.ajax {
    url: target_url,
    data: fd,
    processData: false,
    contentType: "multipart/form-data",
    type: "POST",
    complete: (xhr,status) ->
      console.log xhr.status
      console.log xhr.statusCode
      console.log xhr.responseText

  }
jQuery.get(image_source_url, null, handler)

我怎样才能将此图像作为 blob 检索?

【问题讨论】:

  • 我认为你必须在服务器端更改响应类型。
  • 我正在尝试从任何 url 中提取图像,而不是我必须拥有的服务器。
  • 似乎该答案中的三个解决方案是(1)使用 标签,(2)使服务器以 byte64 编码的形式提供图像或(3)使用浏览器的缓存. (2) 被排除,因为我希望脚本可以使用任何图像 url。我不确定如何使用 (1) 或 (3),因为一旦我下载了图像,我需要将其转换为 Blob。
  • 选项 3 仅在您已经下载图像时才有效。您将第一次需要不同的东西。也许选择 1?

标签: javascript jquery


【解决方案1】:

你不能用 jQuery ajax 做到这一点,但用原生 XMLHttpRequest。

var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(){
    if (this.readyState == 4 && this.status == 200){
        //this.response is what you're looking for
        handler(this.response);
        console.log(this.response, typeof this.response);
        var img = document.getElementById('img');
        var url = window.URL || window.webkitURL;
        img.src = url.createObjectURL(this.response);
    }
}
xhr.open('GET', 'http://jsfiddle.net/img/logo.png');
xhr.responseType = 'blob';
xhr.send();      

编辑

所以重温这个话题,看来用 jQuery 3 确实可以做到这一点

jQuery.ajax({
        url:'https://images.unsplash.com/photo-1465101108990-e5eac17cf76d?ixlib=rb-0.3.5&q=85&fm=jpg&crop=entropy&cs=srgb&ixid=eyJhcHBfaWQiOjE0NTg5fQ%3D%3D&s=471ae675a6140db97fea32b55781479e',
        cache:false,
        xhr:function(){// Seems like the only way to get access to the xhr object
            var xhr = new XMLHttpRequest();
            xhr.responseType= 'blob'
            return xhr;
        },
        success: function(data){
            var img = document.getElementById('img');
            var url = window.URL || window.webkitURL;
            img.src = url.createObjectURL(data);
        },
        error:function(){
            
        }
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
<img id="img" width=100%>

使用 xhrFields 设置 responseType

    jQuery.ajax({
            url:'https://images.unsplash.com/photo-1465101108990-e5eac17cf76d?ixlib=rb-0.3.5&q=85&fm=jpg&crop=entropy&cs=srgb&ixid=eyJhcHBfaWQiOjE0NTg5fQ%3D%3D&s=471ae675a6140db97fea32b55781479e',
            cache:false,
            xhrFields:{
                responseType: 'blob'
            },
            success: function(data){
                var img = document.getElementById('img');
                var url = window.URL || window.webkitURL;
                img.src = url.createObjectURL(data);
            },
            error:function(){
                
            }
        });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
    <img id="img" width=100%>

【讨论】:

  • 谢谢。我只是想通了,看到你的答案。它与我的相似(除了我将其发布到表单而不是将其设置在 对象上)。无论如何我都会把它标记为正确的:)
  • @jabalsad 问了How can I retrieve this image as a blob instead? 的问题,无论如何这只是为了演示它,handler 将采用this.response 并将其添加到 formdata 对象并通过 ajax 发送。
  • +1,效果很好!仅供参考,这可能很快就会被添加到 jQuery 中:github.com/jquery/jquery/pull/1525
  • 2017:jQuery 仍然无法处理类型“blob”?
  • 'xhrFields' 也适用于 jQuery 2(在 2.2.4 中测试)
【解决方案2】:

如果您需要使用 jQuery.AJAX处理错误消息,则需要修改 xhr 函数,以便 responseType发生错误时不会被修改。

所以你必须将responseType修改为“blob”,前提是它是一个成功的调用:

$.ajax({
    ...
    xhr: function() {
        var xhr = new XMLHttpRequest();
        xhr.onreadystatechange = function() {
            if (xhr.readyState == 2) {
                if (xhr.status == 200) {
                    xhr.responseType = "blob";
                } else {
                    xhr.responseType = "text";
                }
            }
        };
        return xhr;
    },
    ...
    error: function(xhr, textStatus, errorThrown) {
        // Here you are able now to access to the property "responseText"
        // as you have the type set to "text" instead of "blob".
        console.error(xhr.responseText);
    },
    success: function(data) {
        console.log(data); // Here is "blob" type
    }
});

注意

如果您在将xhr.responseType 设置为“blob”后立即调试并在该点放置断点,您会注意到,如果您尝试获取responseText 的值,您将获得以下消息:

只有当对象的 'responseType' 是 '' 或 'text'(原为 'blob')时,该值才可访问。

【讨论】:

  • 非常感谢!这是我正在寻找的解决方案。我需要 AJAX 在 .done() 方法中将成功响应作为“blob”(在我的情况下为 ZIP 存档)处理,如果出现问题,.fail() 方法中的响应应作为“文本”处理,否则responseText 是空的等等。这个解决方案非常适合我!
【解决方案3】:

非常感谢@Musa,这是一个将数据转换为 base64 字符串的简洁函数。在获取二进制文件的 WebView 中处理二进制文件(pdf、png、jpeg、docx...)文件时,这可能对您很方便,但您需要将文件的数据安全地传输到您的应用程序中。

// runs a get/post on url with post variables, where:
// url ... your url
// post ... {'key1':'value1', 'key2':'value2', ...}
//          set to null if you need a GET instead of POST req
// done ... function(t) called when request returns
function getFile(url, post, done)
{
   var postEnc, method;
   if (post == null)
   {
      postEnc = '';
      method = 'GET';
   }
   else
   {
      method = 'POST';
      postEnc = new FormData();
      for(var i in post)
         postEnc.append(i, post[i]);
   }
   var xhr = new XMLHttpRequest();
   xhr.onreadystatechange = function() {
      if (this.readyState == 4 && this.status == 200)
      {
         var res = this.response;
         var reader = new window.FileReader();
         reader.readAsDataURL(res); 
         reader.onloadend = function() { done(reader.result.split('base64,')[1]); }
      }
   }
   xhr.open(method, url);
   xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
   xhr.send('fname=Henry&lname=Ford');
   xhr.responseType = 'blob';
   xhr.send(postEnc);
}

【讨论】:

    猜你喜欢
    • 2011-09-15
    • 2013-10-14
    • 2022-10-04
    • 2019-10-03
    • 2012-04-28
    • 2019-11-24
    • 2016-09-11
    • 2018-06-11
    • 1970-01-01
    相关资源
    最近更新 更多