尝试将从谷歌驱动器收到的原始数据保存到服务器

时间:2015-10-16 20:41:48

标签: google-drive-api

我正在提供服务,允许从用户的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提前。

1 个答案:

答案 0 :(得分:0)

最后,我向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);

仍然不知道,为什么无法保存原始数据。希望这有助于某些人