GoogleドライブAPIのマルチパートアップロードファイルは、サイズが1キロバイトを破損し

Muditシン:

私は、GoogleドライブのAPIを使用してファイルをアップロードしようとしてきたが、私はこの問題に直面していると私は解決策を見つけるように見えることはできません。画像をアップロードする場合には、正しいメタデータとサイズ1キロバイトの破損したファイルをアップロードし、アップロード、テキストファイルが、それは、データおよびファイルのない元のデータとして[オブジェクトファイル]のテキストファイルをアップロードします。

$("form").submit(function(evt){  
      evt.preventDefault();
      var formData = new FormData($(this)[0]);
      var control = document.getElementById("csv");
      var files = control.files;
      var dat=files[0];
   $.ajax({
       url: 'https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart',
         type: 'POST',
       contentType: false,
       data: "\n--foo_bar_baz\ \nContent-Type: application/json; charset=UTF-8\ \n\n{ 'name': '"+dat.name+"' }\ \n--foo_bar_baz\ \nContent-Type: "+dat.type+"\ \nContent-Transfer-Encoding: base64\ \n\n"+dat+"\ \n--foo_bar_baz--\ ",
       headers: {
            "Authorization":"Bearer <?php echo $result["access_token"]; ?>",
            "Content-Type": "multipart/related; boundary=foo_bar_baz",
            "Content-Length": formData.length
       },
       async: false,
       cache: false,
       enctype: 'multipart/form-data',
       processData: false,
       success: function (response) {
         var res = JSON.stringify(response);
        console.log("S: "+res);
       },
       error: function(response) {
        var res = JSON.stringify(response);
        console.log("E: "+res);
       }
   });
   return false;
 });
Tanaike:

どのようにこの答えは?いくつかの可能な答えのひとつと考えるしてください。

修正ポイント:

  • あなたは、base64としてファイルをアップロードしようとしています。しかし、あなたのスクリプトでは、datbase64ではありません。
    • この場合、私が使用しましたFileReader
  • 削除してください\\ \nの中で"\n--foo_bar_baz\ \nContent-Type: application/json; charset=UTF-8\ \n\n{ 'name': '"+dat.name+"' }\ \n--foo_bar_baz\ \nContent-Type: "+dat.type+"\ \nContent-Transfer-Encoding: base64\ \n\n"+dat+"\ \n--foo_bar_baz--\ ",

上記のポイントは、あなたのスクリプトに反映されると、次のようになります。

変更したスクリプト:

$("form").submit(function(evt){  
  evt.preventDefault();
  var formData = new FormData($(this)[0]);
  var control = document.getElementById("csv");
  var files = control.files;
  var dat = files[0];

  var f = new FileReader();  // Added
  f.onload = function(){  // Added
    const base64Data = this.result.split(",")[1];  // Added
    $.ajax({
      url: 'https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart',
      type: 'POST',
      contentType: false,
      data: "\n--foo_bar_baz\nContent-Type: application/json; charset=UTF-8\n\n{'name': '"+dat.name+"'}\n--foo_bar_baz\nContent-Type: "+dat.type+"\nContent-Transfer-Encoding: base64\n\n"+base64Data+"\n--foo_bar_baz--\n",  // Modified
      headers: {
        "Authorization": "Bearer <?php echo $result["access_token"]; ?>",
        "Content-Type": "multipart/related; boundary=foo_bar_baz",
        "Content-Length": formData.length
      },
      async: false,
      cache: false,
      enctype: 'multipart/form-data',
      processData: false,
      success: function (response) {
        var res = JSON.stringify(response);
        console.log("S: "+res);
      },
      error: function(response) {
        var res = JSON.stringify(response);
        console.log("E: "+res);
      }
    });
  }
  f.readAsDataURL(dat);  // Added
  return false;
});

注意:

  • ときにuploadType=multipart使用され、最大ファイルサイズは5 MBです。このように注意してください。参考

これはあなたの問題を直接解決しなかった場合、私はお詫び申し上げます。

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=30799&siteId=1