1

phonegap を使用して Android アプリケーションを作成しています。このアプリでは、ユーザーが php ページ (サーバー側) からファイルをダウンロードできるようにしたいと考えています。これは、私の Android プロジェクトの html ページ index.html です。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Download</title>
</head>

<body>

    <form id="down" name="down" action="http://172.25.10.99/test/download.php" method="GET">
    <!-- 172.25.10.99 : server IP -->
        <input type="text" name="filename" id="filename"/>
        <input type="submit" id="id2" value="download"/> 
    </form>
</body>
</html>

そして、ここにphpページ download.php があります:

        <?php

    try {
        $file = "D:\\file\\" . $_REQUEST['filename'];
    } catch (Exception $ex) {
        $file = "D:\\file\\pdf2.pdf";
    }
    $fp = fopen($file, 'r');
    $content = fread($fp, filesize($file));
    fclose($fp);
    header("Accept-Ranges: bytes");
    header("Keep-Alive: timeout=15, max=100");
    header("Content-Disposition: attachment; filename=" . basename($file));
    header("Content-Type: application/octet-stream");
    header("Content-Transfer-Encoding: binary");
    header("Content-Description: File Transfer");

?>

問題は次のとおりです。アプリケーションでダウンロード ボタンを押しても (サーバー上に存在する正しいファイル名を挿入した後)、保存先を尋ねるダウンロード ウィンドウが表示されることを期待しているのに、何も表示されません。要求されたファイル、またはそれよりも優れているのは、自動的にダウンロードされたダウンロード フォルダーで要求されたファイルを見つけることです。問題は php ページのヘッダーのどこかにあると思います。本当に感謝します..

4

2 に答える 2

2

を使用FileTransfer.downloadします。例を次に示します。

function downloadFile(){

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, 
    function onFileSystemSuccess(fileSystem) {
        fileSystem.root.getFile(
        "dummy.html", {create: true, exclusive: false}, 
        function gotFileEntry(fileEntry) {
            var sPath = fileEntry.fullPath.replace("dummy.html","");
            var fileTransfer = new FileTransfer();
            fileEntry.remove();

            fileTransfer.download(
                "http://www.w3.org/2011/web-apps-ws/papers/Nitobi.pdf",
                sPath + "theFile.pdf",
                function(theFile) {
                    console.log("download complete: " + theFile.toURI());
                    showLink(theFile.toURI());
                },
                function(error) {
                    console.log("download error source " + error.source);
                    console.log("download error target " + error.target);
                    console.log("upload error code: " + error.code);
                }
            );
        }, fail);
    }, fail);
};

}

于 2013-08-24T02:24:01.347 に答える
2

この回答は上記の回答に近いですが、よりシンプルで理解しやすいですが、アミット氏の助けがなければこの回答には至らなかったと言わざるを得ませんが、私の解決策は次のとおりです。

<script>
        function downloadFile2()
        {       
        alert("I'm in df2");     
        var fileTransfer = new FileTransfer();
        var uri = encodeURI("http://172.25.10.170/test/download.php?select=img.jpg");
        var filePath = "/mnt/sdcard/img.jpg";
        fileTransfer.download(
            uri,
            filePath,
            function(entry) {
                document.getElementById("id11").innerHTML="download complete: " + entry.fullPath;
            },
            function(error) {
                document.getElementById("id11").innerHTML="download error source " + error.source;
                document.getElementById("id11").innerHTML="download error target " + error.target;
                document.getElementById("id11").innerHTML="upload error code" + error.code;
            },
            true,
            {
            }
        );
        };
</script>
        <button id="downtbtn2" onClick="downloadFile2();" >downloadTest</button>
        <br/>
        <label id="id11">here should be the result</label> 
于 2013-08-26T08:15:34.970 に答える