从Google Drive获取文件并隐藏到Blob

Get file from Google Drive and covert to Blob

本文关键字:隐藏 Blob 文件 Google Drive 获取      更新时间:2023-09-26

我使用这个Google API JS示例中的函数

function downloadFile(file, callback) {
  if (file.downloadUrl) {
    var accessToken = gapi.auth.getToken().access_token;
    var xhr = new XMLHttpRequest();
    xhr.open('GET', file.downloadUrl);
    xhr.setRequestHeader('Authorization', 'Bearer ' + accessToken);
    xhr.onload = function() {
      callback(xhr.responseText);
 };
 xhr.onerror = function() {
      callback(null);
 };
 xhr.send();
 } else {
    callback(null);
 }
}

我获取文件内容,但如何从xhr.responseText创建Blob?

我在这里找到了答案http://www.html5rocks.com/ru/tutorials/file/xhr2/

我不得不使用

 xhr.responseType='blob'

尝试使用本机Blob构造函数。

示例形式MDN:

var aFileParts = ['<a id="a"><b id="b">hey!</b></a>'];
var oMyBlob = new Blob(aFileParts, {type : 'text/html'}); // the blob