Adobe Integrated Runtime (AIR) for JavaScript Developers Pocket Reference

(nextflipdebug5) #1
File API | 85

This example provides a baseline for the various types of
asynchronous access an application might choose to per-
form. In this case, the contents of the file are read and placed
into an HTML text area each time more data is available.
Asynchronous processing also provides the means for ran-
dom file access (seek) without interrupting the user. An
application should always use asynchronous access when-
ever the size of a file is in question:


<html>
<head>

<title>Asynchronous File Access</title>
<script type="text/javascript" src="AIRAliases.js"></
script>

<script>
var file = null;
var stream = null;

function doLoad( )
{
file = air.File.applicationStorageDirectory.resolve(
"myFile.txt" );

stream = new air.FileStream( );
stream.addEventListener( air.ProgressEvent.PROGRESS,
doProgress );
stream.openAsync( file, air.FileMode.READ );
}

function doProgress( event )
{
var data = stream.readMultiByte( stream.
bytesAvailable, air.File.systemCharset );

document.getElementById( "editor" ).value += data;

if( event.bytesLoaded == event.bytesTotal )
{
stream.close( );
}
}
</script>
Free download pdf