Skip to content

Instantly share code, notes, and snippets.

@velara3
Created June 23, 2016 22:29
Show Gist options
  • Save velara3/fbe737b1060c4970f31cc0ebf803a585 to your computer and use it in GitHub Desktop.
Save velara3/fbe737b1060c4970f31cc0ebf803a585 to your computer and use it in GitHub Desktop.
How to loop through files and update the file name using Flex AIR WindowedApplication
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
xmlns:ns="http://flex.apache.org/experimental/ns"
xmlns:file="com.flexcapacitor.effects.file.*"
>
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.events.FlexEvent;
protected function button1_clickHandler(event:MouseEvent):void
{
browseForFile.play();
}
protected function browseForFile_selectHandler(event:Event):void
{
}
protected function loadfile1_totalCompleteHandler(event:Event):void
{
var files:Array = loadFile.filesArray;
var newFile:File;
var data:String;
var fileStream:FileStream = new FileStream();
// Error: Error #3012: Cannot delete file or directory.
// at flash.filesystem::File/copyTo()
// -- error because file already exists and overwrite was false
// Error: Error #2029: This URLStream object does not have a stream opened.
for each (var file:File in files) {
newFile = new File(file.nativePath + ".txt");
file.copyTo(newFile, true);
fileStream.open(newFile, FileMode.WRITE);
data = file.data.readUTFBytes(file.data.length);
data = data.substr(data.lastIndexOf("Content-Description"));
fileStream.writeUTFBytes(data);
fileStream.close();
//newFile.data.writeUTFBytes(data);
trace(data);
}
}
]]>
</fx:Script>
<fx:Declarations><!-- Class declarations below are from the FCLibary -->
<file:BrowseForFile id="browseForFile" allowMultipleSelection="true"
select="browseForFile_selectHandler(event)">
<file:multipleSelectionEffect>
<file:LoadFile id="loadFile" totalComplete="loadfile1_totalCompleteHandler(event)"
filesArray="{browseForFile.fileList}"/>
</file:multipleSelectionEffect>
</file:BrowseForFile>
</fx:Declarations>
<s:Button label="do stuff" click="button1_clickHandler(event)"/>
</s:WindowedApplication>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment