正在尝试将从 google 驱动器接收到的原始数据保存到服务器
Trying to save raw data, which has been received from google drive, to server
我正在制作服务,它允许从用户的 Google 驱动器中选择一些 pdf 文件,然后脚本将文件转换为 jpg。我无法从 Google 驱动器下载文件。
我正在获取这样的文件数据:
function step3process()
{
$("#content").empty();
var pdf = checkedPdf[downloadedCnt];
var i = downloadedCnt;
gapi.client.request({
'path': '/drive/v2/files/'+pdf,
'method': 'GET',
callback: function ( theResponseJS, theResponseTXT ) {
var myToken = gapi.auth.getToken();
var myXHR = new XMLHttpRequest();
myXHR.open('GET', theResponseJS.downloadUrl, true );
myXHR.setRequestHeader('Authorization', 'Bearer ' + myToken.access_token );
myXHR.onreadystatechange = function( theProgressEvent ) {
if (myXHR.readyState == 4) {
if ( myXHR.status == 200 ) {
var rawcont = utf8_to_b64(myXHR.response);
$.ajax({
type: "POST",
beforeSend: function(req) {
req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
},
url: './savepdf.php',
data: rawcont,
dataType: "json"
}
).done(
function (msg) {
downloadedCnt++;
console.log("Скачан файл " + msg.name + " Всего скачано " + downloadedCnt + " из " + checkedPdf.length);
if (downloadedCnt < checkedPdf.length) step3process();
}
);
}
}
};
myXHR.send();
}
});
}
服务器端看起来像:
if (isset($_POST))
{
$pdfData = '';
foreach($_POST as $p)
{
$pdfData .= $p;
}
$filteredData = $pdfData;
$unencodedData = urldecode(base64_decode($filteredData));
$fp = fopen($filepath, "w");
fwrite($fp, $unencodedData);
fclose($fp);
echo("{\"answer\": \"ok\", \"name\": \"{$filepath}\", \"length\": \"" . strlen($unencodedData) . "\" }");
}
毕竟,我得到的是这样的空 pdf:
upload result example
我也注意到了,原来的pdf文件和下载的有一些不同,这里是例子:
files in mcedit
我的解决方案是,如果我可以获得包含内容的 pdf 文件。提前Tnx.
最后,我向 php 提供了令牌:
$.ajax({
type: "POST",
url: './downloadpdf.php',
data: "url="+ encodeURIComponent(downUrl) + "&name="+ encodeURIComponent(pdfNames[pdf]) +"&token=" + myToken.access_token,
dataType: "json"
}).done();
并使用 php 轻松下载文件:
$url = urldecode($_POST['url']);
$name = urldecode($_POST['name']);
$token = $_POST['token'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer '.$token
));
$filepath = "./pdf/{$name}";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
// grab URL
$pdf = curl_exec($ch);
curl_close($ch);
file_put_contents($filepath,$pdf);
还是不懂,为什么不能保存原始数据。希望这对某些人有所帮助1
我正在制作服务,它允许从用户的 Google 驱动器中选择一些 pdf 文件,然后脚本将文件转换为 jpg。我无法从 Google 驱动器下载文件。
我正在获取这样的文件数据:
function step3process()
{
$("#content").empty();
var pdf = checkedPdf[downloadedCnt];
var i = downloadedCnt;
gapi.client.request({
'path': '/drive/v2/files/'+pdf,
'method': 'GET',
callback: function ( theResponseJS, theResponseTXT ) {
var myToken = gapi.auth.getToken();
var myXHR = new XMLHttpRequest();
myXHR.open('GET', theResponseJS.downloadUrl, true );
myXHR.setRequestHeader('Authorization', 'Bearer ' + myToken.access_token );
myXHR.onreadystatechange = function( theProgressEvent ) {
if (myXHR.readyState == 4) {
if ( myXHR.status == 200 ) {
var rawcont = utf8_to_b64(myXHR.response);
$.ajax({
type: "POST",
beforeSend: function(req) {
req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
},
url: './savepdf.php',
data: rawcont,
dataType: "json"
}
).done(
function (msg) {
downloadedCnt++;
console.log("Скачан файл " + msg.name + " Всего скачано " + downloadedCnt + " из " + checkedPdf.length);
if (downloadedCnt < checkedPdf.length) step3process();
}
);
}
}
};
myXHR.send();
}
});
}
服务器端看起来像:
if (isset($_POST))
{
$pdfData = '';
foreach($_POST as $p)
{
$pdfData .= $p;
}
$filteredData = $pdfData;
$unencodedData = urldecode(base64_decode($filteredData));
$fp = fopen($filepath, "w");
fwrite($fp, $unencodedData);
fclose($fp);
echo("{\"answer\": \"ok\", \"name\": \"{$filepath}\", \"length\": \"" . strlen($unencodedData) . "\" }");
}
毕竟,我得到的是这样的空 pdf: upload result example
我也注意到了,原来的pdf文件和下载的有一些不同,这里是例子: files in mcedit
我的解决方案是,如果我可以获得包含内容的 pdf 文件。提前Tnx.
最后,我向 php 提供了令牌:
$.ajax({
type: "POST",
url: './downloadpdf.php',
data: "url="+ encodeURIComponent(downUrl) + "&name="+ encodeURIComponent(pdfNames[pdf]) +"&token=" + myToken.access_token,
dataType: "json"
}).done();
并使用 php 轻松下载文件:
$url = urldecode($_POST['url']);
$name = urldecode($_POST['name']);
$token = $_POST['token'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer '.$token
));
$filepath = "./pdf/{$name}";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
// grab URL
$pdf = curl_exec($ch);
curl_close($ch);
file_put_contents($filepath,$pdf);
还是不懂,为什么不能保存原始数据。希望这对某些人有所帮助1