如何下载我从Google Picker API中选择的文件
How to download a file that I have chosen from Google Picker API?
我正在PHP站点中实现GooglePicker。我可以从Google Picker API获取文件id,也可以使用JavaScript下载文件。下面是我在setCallback(pickerCallback)函数中调用的回调函数。
function pickerCallback(data) {
if (data.action == google.picker.Action.PICKED) {
var fileId = data.docs[0].id;
document.getElementById('googleFileId').value = fileId;
var name = data.docs[0].name;
var url = data.docs[0].url;
var accessToken = gapi.auth.getToken().access_token;
var request = new XMLHttpRequest();
request.open('GET', 'https://www.googleapis.com/drive/v2/files/' + fileId);
request.setRequestHeader('Authorization', 'Bearer ' + accessToken);
request.addEventListener('load', function() {
var item = JSON.parse(request.responseText);
window.open(item.webContentLink,"_self"); //Download file in Client Side
});
request.send();
}
var message = 'File ID of choosen file : ' + fileId;
document.getElementById('result').innerHTML = message;
}
我可以将文件id传递给PHP,但要下载文件,我必须再次进行身份验证。有人能帮助我们如何用PHP进行文件下载吗?
谷歌开发者页面上有一个"管理下载"帮助,但对我不起作用https://developers.google.com/drive/web/manage-downloads.
发现了一个与此类似的问题,但没有回答如何在后台下载文件从谷歌选择器中选择文件后立即下载文件。
您必须为pick操作实现回调。看看我的实现:
var buildPicker = function(parentId) {
var pickerCallback = function(data) {
if (data[google.picker.Response.ACTION] === google.picker.Action.PICKED && data.viewToken[0] !== 'upload') {
var docs = data[google.picker.Response.DOCUMENTS];
for (var d = 0; d < docs.length; d++) {
downloadFile(docs[d].id);
}
}
};
GAuth.getToken().then(function(token) {
var picker = new $window.google.picker.PickerBuilder()
.addView(new google.picker.DocsUploadView().setParent(parentId))
.addView(new google.picker.DocsView().setParent(parentId).setIncludeFolders(true))
.setDeveloperKey(apiKey)
.setOAuthToken(token.access_token)
.setCallback(pickerCallback);
picker.enableFeature(google.picker.Feature.MULTISELECT_ENABLED);
picker.build().setVisible(true);
});
};
var downloadFile = function(fileId) {
getFile(fileId).then(function(file) {
var downloadUrl;
if (angular.isDefined(file.exportLinks)) {
downloadUrl = file.exportLinks['application/pdf'];
} else {
downloadUrl = file.webContentLink;
}
var $idown;
var makeiFrame = function(url) {
if ($idown) {
$idown.attr('src', url);
} else {
$idown = $('<iframe>', {
id: 'idown',
src: url
}).hide().appendTo('body');
}
};
makeiFrame(downloadUrl);
});
};
// Implemented with https://github.com/maximepvrt/angular-google-gapi. But any other implementation will be fine as well
var getFile = function(fileId) {
var parameters = {
'fileId': fileId
};
return GApi.executeAuth('drive', 'files.get', parameters);
};
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
相关文章:
- 如何更改bigquery API中的计费层选项
- Amazon S3 REST API大小不正确
- 客户端服务器REST API captcha实现
- 使用Facebook live API创建实时视频对象时的隐私设置
- 谷歌放置API:按国家或餐馆名称搜索餐馆
- MeteorJS:在带有回调的vzaar api上正确使用wrapAsync
- 在事件上打开Materialize Date Picker
- 是否可以控制获取哪些Google地图脚本(JavaScript API)
- API密钥使用和检查示例
- 谷歌地图API v3不适用于移动浏览器或PhoneGap
- 任何方式使AJAX调用Gmail API,而无需通过JS库
- 谷歌地图JS API+JSON-多个标记没有显示
- 单击超链接时,如何使用Google Maps API v3缩放地图
- 如何在 API 调用后和 if 语句中启用提交按钮
- 如何下载我从Google Picker API中选择的文件
- Google Picker API -- 按钮点击事件
- Google Picker API DocsUploadView setIncludeFolders not worki
- 从rails中的Google Picker API下载文件
- 如何在用户按下按钮或图像后显示Google Picker API对话框
- NoGray Time Picker conflict with Google Maps api