Cocoon tries to be as HTML5 compliant as possible but sometimes, as we have full control over the JS environment, we see opportunities to add cool features that are not directly available in the standard APIs. In this case we have added a very simple extension function to Canvas+ XMLHttpRequest object to be able to save the transferred data to a file in the device file system. The function is called cocoonSetOutputFile.

It receives two parameters:

  • FILE_NAME: The name of the file that will be created to store the data that will be downloaded along with the XHR request.
  • STORAGE_TYPE: The type of storage. check out the possible values here.

And it returns:

  • FULL_FILE_PATH_URL_TO_STORED_FILE: A file (file://) URL string to the file that is created and that will store the data downloaded along with the XHR request.

Here is a simple example of how to use this extension function to download audio files to the device so they can be played afterwards.

This code also takes into account that the extension function may not exist (for older version of Canvas+ or to make the code compatible with non Canvas+ execution environments like browsers).