JavaScript从Google drive或Dropbox上的txt文件读取所有文本

JavaScript从Google drive或Dropbox上的txt文件读取所有文本,javascript,google-drive-api,xmlhttprequest,text-files,dropbox,Javascript,Google Drive Api,Xmlhttprequest,Text Files,Dropbox,我想使用JavaScript将Google Drive或Dropbox上的txt文件的全部内容读取到字符串变量中。我见过类似的问题,但我希望完全用JavaScript完成。我已经尝试了一些方法,但我一直收到“XMLHttpRequest无法加载…没有‘访问控制允许源代码’”和“无法执行‘发送’”错误。我也希望在没有任何外部API或安装的情况下执行此操作。这里有一个javascript函数,可以从中下载文本文件。 使用fetchapi function get_doc(id){ con

我想使用JavaScript将Google Drive或Dropbox上的txt文件的全部内容读取到字符串变量中。我见过类似的问题,但我希望完全用JavaScript完成。我已经尝试了一些方法,但我一直收到“XMLHttpRequest无法加载…没有‘访问控制允许源代码’”和“无法执行‘发送’”错误。我也希望在没有任何外部API或安装的情况下执行此操作。

这里有一个javascript函数,可以从中下载文本文件。 使用fetchapi

  function get_doc(id){
    const url = 'https://www.googleapis.com/drive/v3/files/'+id+'?alt=media'
    if(self.fetch){
    var setHeaders = new Headers();
    setHeaders.append('Authorization', 'Bearer ' + authToken.access_token);
    setHeaders.append('Content-Type', mime);

    var setOptions = {
        method: 'GET',
        headers: setHeaders
    };
    fetch(url,setOptions)
        .then(response => { if(response.ok){
        var reader = response.body.getReader();
        var decoder = new TextDecoder();
        reader.read().then(function(result){
            var data = {}
            data = decoder.decode(result.value, {stream: !result.done});
            console.log(data);
    });
        }
    else{
        console.log("Response wast not ok");
    }
  })  .catch(error => {
        console.log("There is an error " + error.message);
        });
    }
}

如果是谷歌文档文件,请参阅

s007
answer工作得很好,但我不知道为什么它不返回文件中的所有文本(有些文本被删除)。下面我使用的是来自google drive javascript api的
文件.get
函数。因此,在您进行身份验证后,此功能将起作用

this.readFile = function(fileId,callback){
    var request = gapi.client.drive.files.get({
      fileId: fileId,
      alt: 'media'
    })
    request.then(function(response){
        console.log(response); //response.body has the text content of the file
        if (typeof callback === "function") callback(response.body); 
    },function(error){
        console.error(error)
    })
    return request; //for batch request
}

我得到的自我是没有定义的。如何定义它