Using File System in Adobe Flex
Posted By : Ankush Gulati | 11-Apr-2013
Flex provides an easy solution for working with the File system. A File Object is a pointer to a file or directory. The File class class provides several useful methods for use such as creation, copying, moving, deleting or modifying the files and directories.
Defining the File Object:
1. Pointing the file location manually:
You can provide the url to file manually by creating the file object and passing url as argument like:
var file:File = new File("file:///C:/users/userName/fileName.txt");
The file object will contain a pointer to this new file. If there exists a blank space in url, replacing it with "%20" makes it usable.
2. Making the user browse to a File:
The file class includes methods such as browseForOpen() and browseForSave() which automatically opens up system dialog box for selecting the particular file.
A simple example for "Open" dialog box code is below:
var selectedFile:File = new File();
var txtFilter:FileFilter = new FileFilter("Text", "*.as;*.css;*.html;*.txt;*.xml");
selectedFile.browseForOpen("Open", [txtFilter]);
selectedFile.addEventListener(Event.SELECT, fileSelected);
function fileSelected(event:Event):void
{
Alert.show(selectedFile.nativePath);
//place code for file opeartion here...
}
Using the File object:
Once you get a hold of file object, you can use it's properties to reveal further details like creationDate, extension, icon, isDirectory, size or type etc.
1. File External Operations:
The functions "copyTo()", "deleteFile()", "moveToTrash()" and "moveTo()" are used to carry out desired operations.
A simple example to copy-paste the file is explained below:
var originalFile:File = new File("file:///C:/users/userName/originalFileName.txt");
var duplicateFile:File = new File("file:///C:/users/userName/duplicate.txt");
originalFile.addEventListener(Event.COMPLETE, fileCopyCompleteHandler);
function fileCopyCompleteHandler(event:Event):void {
Alert.show(event.target+" has been created.");
}
originalFile.copyTo(duplicateFile, true);
The function "createTempFile()" can be used to create the temporary files for caching purpose.
2. File Creation and Manipulation Operations:
The FileStream object is used for reading, writing, appending or updating the document. FileMode need to specified to FileStream object.
A simple example to write a string in a file is below:
var srcFile:File = new File("file:///C:/users/userName/originalFileName.txt");
var srcFileStream:FileStream = new FileStream();
srcFileStream.open(srcFile, FileMode.WRITE);
srcFileStream.writeUTFBytes("hello");
Reading bytes from file:
A simple example to read the file in form of bytes and from a specific position is explained below:
var bytesRead:ByteArray = new ByteArray();
var srcFile:File = new File("file:///C:/users/userName/originalFileName.txt");
var srcFileStream:FileStream = new FileStream();
srcFileStream.open(srcFile, FileMode.READ);
srcFileStream.position = 10;
srcFileStream.readBytes(bytesRead, 0, 20);
The above code snippet will read 20 bytes from the specifies position.
Flex also provide other useful methods such as "Asynchronous operations" which are not described in this blog but can be quite useful depending upon user's requirement.
Hope it helps :)
Ankush Gulati
[email protected]
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Ankush Gulati