dongyin4202 2013-08-23 16:07
浏览 35
已采纳

如何使用phonegap(Android平台)从php页面下载文件?

I'm using phonegap to create an Android application ,in this app I'd like to allow the user to download a file from a php page (server side) ,that's where I'm having troubles . Here is the html page index.html from my Android project :

<!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>

And here is the php page 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");

?>

The problem is : I'm getting nothing when I press the download button in my application (after inserting a file name which is correct and exists on the server), while I'm expecting to see a download window that asks me where to save the requested file ,or better than that ,to find the requested file in the download folder downloaded automatically ,and I believe that the problem is somewhere in the header in the php page.Could you please help ? I'd really appreciate it ..

展开全部

  • 写回答

2条回答 默认 最新

  • dsf487787 2013-08-23 18:24
    关注

    Use FileTransfer.download, here is an example:

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

    }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部