1

我在我的网络应用程序中使用谷歌选择器来允许用户从他的谷歌驱动器中浏览和选择文件。一旦他做出选择,picker 就会返回有关所选文件的各种数据,包括文件 ID 和 URL。我的目标是将选定的文件下载到服务器。如果我将 URL 传递给我的后端脚本,它会下载文件但它们已损坏,这是我的代码:

回调函数:

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

这是将数据发送到我的 php 文件的函数:

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 函数:

        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];
    }别的 {
        $url = $_POST['datatable'] 。'&e=下载';
        $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 。'/Myfiles1/' 。$标题。'。' . $文件扩展;
    header("Pragma: public");
    header("过期时间:0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("缓存控制:公共");
    header("内容描述:文件传输");
    header("内容类型:" . $type . "");
    header("Content-Disposition: attachment; filename=\"" . $title . '.' . $fileext . "\"");
    header("内容传输编码:二进制");
    header("内容长度:" . $filesize);
    // 保存下载文件的文件夹。必须以斜线结尾
    $destination_folder = $upload_path 。'/Myfiles1/';
    $newfname = $destination_folder 。basename($title . '.' . $fileext);
    $file = fopen($url, "rb");
    如果($文件){
        $newf = fopen($newfname, "wb");
        如果 ($newf)
            而 (!feof($file)) {
                fwrite($newf, fread($file, 1024 * 8), 1024 * 8);
            }
    }
    如果($文件){
        fclose($文件);
    }
    如果($newf){
        fclose($newf);
    }
    ob_end_flush();

关于如何下载文件而不损坏文件的任何想法?

4

0 回答 0