Click here to Skip to main content
14,699,089 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My Script:
<script>
           
            var myTrigger;
            var progressElem = $('#progress');
            $.ajax({
                type: 'GET',
                url: 'download.php',
                
                beforeSend: function(thisXHR)
                {
                    myTrigger = setInterval(function()
                    {
                        if (thisXHR.readyState > 2)
                        {
                            var totalBytes = thisXHR.getResponseHeader('Content-length');
                            var dlBytes = thisXHR.responseText.length;
                            (totalBytes > 0) ? progressElem.html(Math.round((dlBytes / totalBytes) * 100) + "%") : progressElem.html(Math.round(dlBytes / 1024) + "K");
                        }
                    }, 200);
                },
                complete: function()
                {
                    clearInterval(myTrigger);
                },
                success: function(response)
                {
                    // Process XML
                }
            });
                     
        </script>


My HTML:

<body>
        <form  enctype="multipart/form-data">
            <iframe style="display: none"></iframe>
            <div id="progress" style="width:500px;border:1px solid #ccc;"></div>
            <!-- Progress information -->
            <div id="information" style="width"></div>
            <input type="text" name="file">
            <button id="download" >Download</button>
     <!--<input type="file" name="file" id="file"><br>
     <input type="submit" name="submit" value="Submit">-->
        </form>

    </body>


My PHP:
<?php

if(empty($_POST['file'])){
	exit;
}

// Sanitizing the filename:
$filename = $_POST['file'];

// Outputting headers:
header("Cache-Control: ");
header("Content-type: application/octet-stream");
header("Content-Description: File Transfer");
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . filesize($filename));
header('Content-Disposition: attachment; filename="'.$filename.'"');
  readfile($filename);



?>

I don't know how to call this JavaScript in my "download" button. And I don't know how to calculate total size of my file and how many bytes are downloaded in client pc.

It will helpful for me if you make my code as required it have to do.
Posted
Comments
Prasad Khandekar 8-Apr-13 10:11am
   
You can not use AJAX for download.

1 solution

let the browser handle its download issue. and use the below code:

<button id="download" onclick='window.location="download.php?file=filename"'>Download</button>


And from your download.php use $_GET method not $_POST.

you can also try POST method with ajax, but if I try to give you proper method then i will have to test it first. what you can do is(as test) create a div as hidden with some

on

success: function(response)
{
   //my jquery can be wrong but you can look for the right one
   $(document).append("<div id="SomeCrazyId" style="display:none"></div>");
   $("#SomeCrazyId").html(response);
}


and see what happen
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900