0

I want to transfer file from Google Drive to my Server Directly when any user selects a file from Google Drive Picker. I am using following code but the file does not get downloaded in my particular folder when using following code..

Callback Function:

function onPickerAction(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 to download File:

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 = [url, title, type, filesize, fileext,id];
    document.getElementById("myddlink").href=link; 
    request.open("POST", "downloadfile.php?" + datatable, true);
    request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    request.send("datatable=" + datatable);
}

Php File:

$upload_path='upload_folder';
    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-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

user3653474
  • 3,393
  • 6
  • 49
  • 135
  • Do you encounter any errors? Check the sharing settings for the file or folder to ensure the file can be reached. – Twisty Sep 11 '15 at 17:22
  • No i am not getting any errors. Is there any mistake in path of the folder to save the file.... – user3653474 Sep 11 '15 at 17:25
  • When the POST occurs, what does your browser show in it's console for the post? I don't see where `exportFormat` is defined when you post data to the PHP. So it would default to `datatable`, and I don't see how this is serialized. I think this is the issue. – Twisty Sep 11 '15 at 17:43
  • Can u please give any link of the above.. – user3653474 Sep 11 '15 at 18:06
  • because i am new to google drive please help to solve my problem. – user3653474 Sep 11 '15 at 18:08
  • See here: http://stackoverflow.com/questions/14873443/sending-an-http-post-using-javascript-triggered-event and then consider `var datatable = { "url":url, "title":title, "type":type, "filesize":filesize, "fileext":fileext, "id":id };` – Twisty Sep 11 '15 at 20:55
  • Thanks Twisty for your suggestion i tried it but it is not working, Can u edit my code to make it work properly. From the above code what i get is an ASCII HTML Document every time i download image after selecting from google drive picker, I have added an image of that. – user3653474 Sep 13 '15 at 07:00
  • I can't just write the code for you. I also have no way to test the code. – Twisty Sep 14 '15 at 15:26

0 Answers0