addToMediaCacheExt Method

This command will get a file from the Internet and cache it locally on the device.

intel.xdk.cache.addToMediaCacheExt(url,id);

Description:

This command will get a file from the Internet and cache it locally on the device. It can then be referenced in a special directory named _mediacache off the root of the bundle. As this method is executed, the "intel.xdk.cache.media.update" event is fired repeatedly to track the progress of the file caching. If there is already a file cached with that name it is overwritten. As the file is cached by this method, a unique id is returned in order to identify their origin. This command will replace the depreciated command addToMediaCache.

Available Platforms:

Parameters:

  • url: The url of the file to cache.
  • id: A unique identifier for the cache request.

    Events:

  • intel.xdk.cache.media.update: This event is fired as this method runs. It will return an event object that contains several parameters. The first parameter is the URL of the remote file cached. The second is the unique ID assigned when the command was called. The third is the current number of bytes downloaded and cached so far, and the final parameter is the total number of bytes in the file.
  • intel.xdk.cache.media.add: This event is fired once the file is successfully cached.

    Example:

    
    intel.xdk.cache.addToMediaCacheExt(urlToCache,uniqueID); 
    
    function cacheUpdated(evt)
    {
            var outString = "";
            outString += "current bytes downloaded: " + evt.current; 
            outString += " total bytes in download: " + evt.total;
            var percentage = evt.current/evt.total
            outString += " percentage downloaded: " + percentage + "%";
            outString += " the unique id is: " + evt.id ;
            outString += "the URL is: " + evt.url;
            alert(outString);
    }
    
    function cacheComplete(evt)
    {
      var outString = "";
      outString += "The procedure succeeded (" + evt.success + ") ";
            outString += " the unique id is: " + evt.id ;
            outString += "the URL is: " + evt.url;
            alert(outString);
    }
    
    document.addEventListener("intel.xdk.cache.media.update", cacheUpdated, false);
    document.addEventListener("intel.xdk.cache.media.add", cacheComplete, false);
                
    
For more complete information about compiler optimizations, see our Optimization Notice.