2013-08-24 00:07
浏览 35


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" "">
<html xmlns="">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />


    <form id="down" name="down" action="" method="GET">
    <!-- : server IP -->
        <input type="text" name="filename" id="filename"/>
        <input type="submit" id="id2" value="download"/> 

And here is the php page download.php :


    try {
        $file = "D:\\file\\" . $_REQUEST['filename'];
    } catch (Exception $ex) {
        $file = "D:\\file\\pdf2.pdf";
    $fp = fopen($file, 'r');
    $content = fread($fp, filesize($file));
    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-24 02:24

    Use, here is an example:

    function downloadFile(){
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, 
        function onFileSystemSuccess(fileSystem) {
            "dummy.html", {create: true, exclusive: false}, 
            function gotFileEntry(fileEntry) {
                var sPath = fileEntry.fullPath.replace("dummy.html","");
                var fileTransfer = new FileTransfer();
                    sPath + "theFile.pdf",
                    function(theFile) {
                        console.log("download complete: " + theFile.toURI());
                    function(error) {
                        console.log("download error source " + error.source);
                        console.log("download error target " +;
                        console.log("upload error code: " + error.code);
            }, fail);
        }, fail);


    打赏 评论
  • dongpao5261 2013-08-26 08:15

    This answer is close to the answer above but is simpler and easier to understand ,although I must say I wouldn't come to this answer without Mr. Amit help ,here is my solution :

            function downloadFile2()
            alert("I'm in df2");     
            var fileTransfer = new FileTransfer();
            var uri = encodeURI("");
            var filePath = "/mnt/sdcard/img.jpg";
                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 " +;
                    document.getElementById("id11").innerHTML="upload error code" + error.code;
            <button id="downtbtn2" onClick="downloadFile2();" >downloadTest</button>
            <label id="id11">here should be the result</label> 
    打赏 评论

相关推荐 更多相似问题