0

Is it possible to detect - via Javascript or a different way - when a file transfer (browser downloading a requested file) is actually really complete?

If the browser requests a file, e.g. through an Ajax call, the success message (status 200, readystate 4) comes already before the file has actually been downloaded. But I need to know, when the transfer itself is completed. Any way to achieve this?

Thanks.

Don Roby
  • 40,677
  • 6
  • 91
  • 113
tricon
  • 313
  • 4
  • 18

1 Answers1

0

I used this code to check if an image is completly loaded:

var newImage;

newImage = new Image();
newImage.onload = function() {
    var domImg;
    domImg = document.getElementById('my_img');
    domImg.src = this.src;

    domImg.width = this.width;
    domImg.height = this.height;
}

newImage.width = <img width>;
newImage.height = <img height>;
newImage.src = <your source>;

Edit: Here is a Fiddle

Marvin
  • 539
  • 5
  • 17
  • Thanks for the quick reply Marvin. I should specify that the file to be transfered is not an image but a font-file (.eot, .svg, .ttf or .woff) and that the file transfer is not initiated by a manual ajax call but "automatically" by the browser, when a dom element is updated to use a font, which is up to that point not used anywhere on the page. Any Idea how to track that? – tricon Apr 23 '14 at 13:17
  • Maybe this thread could help you http://stackoverflow.com/questions/12312323/how-to-know-if-a-font-font-face-has-already-been-loaded – Marvin Apr 23 '14 at 14:46
  • Thanks again for your answer. The problem with all the scripts out there is that they usually test the width of spans containing a text periodically. Running intervals like that is pretty massive on the work-load side. I am trying to come up with a solution on my own I guess, until the w3c releases the font-load api. – tricon Apr 24 '14 at 14:18