jQueryのajaxメソッドを使用して画像をblobとして取得する


84

私は最近、別の(関連する)質問をしました。これは、このフォローアップの質問につながり ます。入力フォームのファイルの代わりにデータを送信する

jQuery.ajax()のドキュメント(http://api.jquery.com/jQuery.ajax/)を読むと、受け入れられたdataTypeのリストに画像が含まれていないようです。

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として取得するにはどうすればよいですか?


サーバー側で応答タイプを変更する必要があると思います。
エリックフリック2013

必ずしも所有しているサーバーではなく、任意のURLからイメージをプルしようとしています。
jabalsad 2013


その答えの3つの解決策は、(1)<img>タグを使用する、(2)サーバーにbyte64エンコードとして画像を提供させる、または(3)ブラウザーのキャッシュを使用することのようです。(2)スクリプトで任意の画像URLを機能させたいため、除外されています。(1)または(3)の使用方法がわかりません。画像をダウンロードしたら、それをBlobに変換する必要があるためです。
jabalsad 2013

オプション3は、イメージを既にダウンロードしている場合にのみ機能します。初めて、何か違うものが必要になります。多分1を選ぶ?
エリックフリック2013

回答:


146

これはjQueryajaxでは実行できませんが、ネイティブ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();      

編集

したがって、このトピックを再検討すると、jQuery3でこれを行うことは確かに可能であるように思われます

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%>


ありがとう。私はそれを理解し、あなたの答えを見ました。これは私のものと似ています(<img>オブジェクトに設定する代わりにフォームに投稿することを除けば)。とにかく正しいとマークします:)
jabalsad 2013

@jabalsad質問が尋ねHow can I retrieve this image as a blob instead?、それをデモしたこととにかく、handler取るthis.responseとFORMDATAオブジェクトに追加し、AJAXを経由して送信します。
ムサ

2
+1、うまくいきます!そして参考
lambshaanxy

9
2017:jQueryはまだタイプ「blob」を処理できませんか?
robsch 2017年

1
'xhrFields'は、jQueryの2で動作します(2.2.4でテスト)
Bampfer

15

あなたがする必要がある場合はエラーメッセージを処理使って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」)の場合のみです。


1
どうもありがとうございました!これが私が探していた解決策でした。.done()メソッドで成功した応答を「blob」(私の場合はZIPアーカイブ)として処理するにはAJAXが必要でした。問題が発生した場合は、メソッドの応答.fail()を「テキスト」として処理する必要responseTextがあります。それ以外の場合は空でした。これはソリューションは私にとって完璧に機能しました!
informatik

4

@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);
}
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.