【发布时间】: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