使用JavaScript下载谷歌驱动器文件 [英] Download Google Drive file using javascript

查看:381
本文介绍了使用JavaScript下载谷歌驱动器文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用JavaScript来我的服务器上下载谷歌驱动器文件当我点击谷歌驱动器选择器,但不能下载的文件。我一直在寻找,如果4天,但问题是低于相同的是,我现在用的是code。

I want to download Google Drive file using javascript to my server when i click on Google Drive picker but cannot get the file downloaded. I have been searching if for 4 days but the issue is same below is the code that i am using.

 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();

输入图像的描述在这里

推荐答案

您需要序列数据表为URL连接codeD字符串第一。

TLDR;

You need to serialize datatable into a URL-encoded string first.

VAR数据表= [URL,标题,类型,文件大小,fileext,ID];

在这里,你正在创建数据表作为数组

Here, you're creating datatable as an array

request.open(POST,AJAX-DB-文件/ copy_drive_file.php?+数据表,真);

request.send(数据表=+数据表);

那么,在这两条线,尝试直接将数组的字符串,它通过调用蒙上数组为字符串。加入('')在阵列

Then, in these two lines, you try to concatenate the array directly to the string, which casts the array to a string by calling .join(',') on the array.

您需要的是序列化数组URL-CN codeD符号。如果你正在使用jQuery,尝试 VAR数据表= $ .PARAM(网址:{url:URL,标题:标题:A型,文件大小:文件大小,fileext:fileext,ID:ID]); 代替。

What you need is to serialize the array into URL-encoded notation. If you're using jQuery, try var datatable = $.param({ url: url, title: title, type: type, filesize: filesize, fileext: fileext, id: id]); instead.

这篇关于使用JavaScript下载谷歌驱动器文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆