2

I would like to download a 512 kb file and check the download time. I use the following codes, however it checks just response time of website and is not waiting until entire file is downloaded. How can I check entire file download time?

var start = new Date();  
$.ajax ({
    url: 'https://www.example.com/512kb',
    cache: false,
    success : function()
    {
        var total=(new Date() - start)  
    },
    error : function(jqxhr, status, ex) {}   
});

Edit: When I run the website at my local PC, internet explorer is waiting until all file is downloaded, Firefox and chrome not. If I put to server all behaves same and not waiting for all file. Why just at local side internet explorer behaves like that?

user1874941
  • 3,013
  • 4
  • 20
  • 31

2 Answers2

0

It looks like you're not using the total variable for anything. Because it is defined within the success callback function, it only exists in that function and is therefore lost when the function returns.

The following code shows an alert box with the time required to complete the ajax call and the length of the data retrieved:

var start = new Date();
var total;
$.ajax ({
    url: 'http://fiddle.jshell.net/',
    cache: false,
    success : function(data)
    {
        total=(new Date() - start);
        alert('total: ' + total + ' len: ' + data.length);
    },
    error : function(jqxhr, status, ex) {}   
});

http://jsfiddle.net/WkKE2/2/

tiffon
  • 5,040
  • 25
  • 34
  • I am using total variable for many things. In question I focused on the method. – user1874941 Jan 09 '13 at 09:11
  • The success callback is executed when the HTTP GET request is responded but not when the file has finished downloading. – FerCa Jan 09 '13 at 09:15
  • @user1874941 In the question, in the `success` callback function you have `var total = ...` which means a `total` variable is created for use within that function. This `total` variable that is created will shadow any other `total` variable that is exposed to the `success` callback function. It may be that you only need to delete the `var` keyword from your callback function. – tiffon Jan 09 '13 at 09:18
  • 2
    @FerCa According to the [jQuery API documentation](http://api.jquery.com/jQuery.ajax/), the first parameter of the `success` callback function is the data returned. I don't understand how the `success` callback function would fire if the data hadn't finished downloading. An excerpt of the API doc: _A function to be called if the request succeeds. The function gets passed three arguments: The data returned from the server,..._ – tiffon Jan 09 '13 at 09:21
0

There is no way to do this in javascript. But you will find some workarrounds in this stackoverflow question: Browser event when downloaded file is saved to disk

EDIT: This other stackoverflow question has a answer that is a better and simpler aproach: detecting when the "File download" popup is closed, Basicaly you target a download link to a hidden iframe in your page and use the onload event for this iframe.

Community
  • 1
  • 1
FerCa
  • 2,067
  • 3
  • 15
  • 18