在适用于iOS和Android的phonegap应用程序中下载pdf文件

时间:2018-08-27 15:51:30

标签: javascript php cordova-plugins

我有一点情况。我不确定为什么此代码无法正常工作。请帮我看看。我正在使用phonegap构建一个移动应用程序,并且正在使用cordova-plugin-file。这是我的JavaScript和PHP代码。

JavaScript

var nurlink = 'https://..../appery/downloadCurriculum.php?source=nurs'; // link to switch php program 
//=====================================
function getSampleFileNur(dirEntry) {
statusMsg.innerHTML = 'Nursery: Downloading..';   // div button triggers request for nursery

    var xhr = new XMLHttpRequest();
    xhr.open('GET', nurlink, true);
    xhr.responseType = 'blob';

    xhr.onload = function(){
        if (this.status == 200) {
            var blob = new Blob([this.response], { type: 'application/pdf' });
            saveFile(dirEntry, blob, "curriculum.pdf");
            statusMsg.innerHTML = this.responseText;  
        }
    };
    xhr.send();
}

和PHP

    <?php header('Access-Control-Allow-Origin: *'); ?>
    <?php ob_start(); ?>
    <?php session_start(); ?>
    <?php error_reporting(0); ?>
    <?php #error_reporting(E_NOTICE | E_ALL); ?>
    <?php ini_set('display_errors', 0); ?>

    <?php
    if($_GET['source']){
        $source = $_GET['source'];

    }

    switch($source){

        case 'nursery':
        $file_url = 'https://www.---/curriculum_Jul28_1b640.pdf';
        header('Content-Type: application/pdf');
        header("Content-Transfer-Encoding: Binary"); 
        header("Content-disposition: attachment; filename=\"" . basename($file_url) . "\""); 
        readfile($file_url);
        echo 'Downloading Curriculum for Nursery '.$month.' '.$year2;
        break;

        case 'preteens':
        $file_url = 'https://www..../curriculum_Jul203bad8.pdf';
        header('Content-Type: application/pdf');
        header("Content-Transfer-Encoding: Binary"); 
        header("Content-disposition: attachment; filename=\"" . basename($file_url) . "\""); 
        readfile($file_url);
        echo 'Downloading Curriculum for Preteens '.$month.' '.$year2;
        break;

        default:
            echo 'Download is starting.';
    }
?>

谢谢大家

0 个答案:

没有答案
相关问题