gpt4 book ai didi

javascript - 使用 JavaScript 下载 Google Drive 文件

转载 作者:行者123 更新时间:2023-11-28 06:56:21 26 4
gpt4 key购买 nike

当我单击 Google Drive 选择器时,我想使用 javascript 将 Google Drive 文件下载到我的服务器,但无法下载该文件。我已经搜索了 4 天,但问题是相同的,下面是我正在使用的代码。

 function pickerCallback(data) {
if (data.action == google.picker.Action.PICKED) {
var id = data.docs[0].id;
var request = new XMLHttpRequest();
request.open('GET', 'https://www.googleapis.com/drive/v2/files/' + id);
request.setRequestHeader('Authorization', 'Bearer ' + gapi.auth.getToken().access_token);
request.addEventListener('load', function() {
var item = JSON.parse(request.responseText);

console.log(item);

downloadFile(item);
});
request.send();
}
}


function downloadFile(item) {
var request = new XMLHttpRequest();
var mimeType = item['mimeType'];
if (typeof item.exportLinks != 'undefined') {
if (mimeType == 'application/vnd.google-apps.spreadsheet') {
mimeType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
}
url = item['exportLinks'][mimeType];
link = url;
} else {
lien = item['downloadUrl'].split("&");
link = lien[0] + '&' + lien[1];
url = item['downloadUrl'];
}
title = item['title'];
type = mimeType;
filesize = item['fileSize'];
fileext = item['fileExtension'];
id = item['id'];


var datatable = $.param({ url: url, title: title, type: type, filesize: filesize, fileext: fileext, id: id});
request.open("POST", "ajax-db-files/copy_drive_file.php?" + datatable, true);
request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
request.send("datatable=" + datatable);

}

php 文件是:

$upload_path='sss';
if (isset($_POST['exportFormat'])) {
$pieces = explode(",", $_POST['exportFormat']);
$url = $_POST['datatable'] . '&exportFormat=xlsx';
$title = $pieces[1];
$type = $pieces[2];
$fileext = $pieces[0];
$fileId = $pieces[5];
}else {
$url = $_POST['datatable'] . '&e=download';
$pieces = explode(",", $_POST['gd']);
$onlytitle = explode(".", $pieces[1]);
$title = $onlytitle[0];
$type = $pieces[2];
$filesize = $pieces[3];
$fileext = $pieces[4];
$fileId = $pieces[5];

}
$fullPath = $upload_path.'/'.$title.'.'. $fileext;
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
//header("Content-type: " . $type . "");
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"".$title.'.'.$fileext."\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . $filesize);
// folder to save downloaded files to. must end with slash
$destination_folder = $upload_path.'/';
$newfname = $destination_folder . basename($title . '.' . $fileext);
$file = fopen($url, "rb");
if ($file) {
$newf = fopen($newfname, "wb");
if ($newf)
while (!feof($file)) {
fwrite($newf, fread($file, 1024 * 8), 1024 * 8);
}
}
if ($file) {
fclose($file);
}
if ($newf) {
fclose($newf);
}
ob_end_flush();

enter image description here

最佳答案

TLDR;

您需要序列化datatable首先转换为 URL 编码的字符串。

<小时/>

说明

var datatable = [url, title, type, filesize, fileext,id];

在这里,您将数据表创建为数组

request.open("POST", "ajax-db-files/copy_drive_file.php?" + datatable, true);

request.send("datatable=" + datatable);

然后,在这两行中,您尝试将数组直接连接到字符串,这通过调用 .join(',') 将数组转换为字符串。在数组上。

您需要将数组序列化为 URL 编码表示法。如果您使用 jQuery,请尝试 var datatable = $.param({ url: url, title: title, type: type, filesize: filesize, fileext: fileext, id: id]);相反。

关于javascript - 使用 JavaScript 下载 Google Drive 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32536108/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com