【问题标题】:Download Google Drive file from url and send with post to backend从 url 下载 Google Drive 文件并通过帖子发送到后端
【发布时间】:2017-11-27 12:31:26
【问题描述】:

我正在尝试从 javascript 中的 url(chrome 驱动器文件)下载文件;并想将其发送到我的后端(php - laravel)。

var url = file.downloadUrl !== undefined ? file.webContentLink : file.exportLinks['application/pdf'];

console.log(url) // if I go to url, it downloads the file

if (url !== undefined) {
      var remote = new XMLHttpRequest();
      remote.open('GET', url);
      remote.setRequestHeader('Authorization', 'Bearer ' + gapi.client.getToken().access_token);
      remote.setRequestHeader('Access-Control-Allow-Origin', '*');
      remote.onload = function(e) {
          vm.handle_download(remote.responseText, file, 200); // do something with the fetched content;
      };
      remote.onerror = function(e) {
             vm.handle_download('error response', null, remote.statusText);
      };
      remote.send();
} else vm.handle_download('no downloadable url', {
     file: null,
     status: 'error'
});

正在处理

handle_download: function (content, file, status) {
        if (status !== 200) {
            console.log('error occured on status')
            return;
        }
}

无法加载https://drive.google.com/uc?id=1D12321ofd4CNG-m9_Mp4aiDcnibNf&export=download:对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头。 Origin 'http://test.dev' 因此不允许访问。

【问题讨论】:

    标签: javascript php google-api google-drive-api


    【解决方案1】:

    这是由于网络中的same origin policy 而导致的预期行为。由于您这样做是出于测试目的,请尝试使用此 Allow-Control-Allow-Origin chrome 扩展。

    您可以在Using CORS tutorial 中阅读有关如何实现此功能的更多信息。 这个SO post 也可能提供更多见解。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-30
      • 2019-11-17
      相关资源
      最近更新 更多