I have made progress on this small project but still having what I think will be the last issue.
The flex app starts by using HTTPRequest to invoke a php script on the server side. The PHP gathers the name of all of the files in the directory and puts them into an XML file that is passed back to Flex. Flex then loads the XML into a datagrid where the user can select the file they wish to download.
I’m using FileReference to attempt doing the downloads and have incorporated the tutorial example from
http://blog.flexexamples.com/2007/07...ference-class/ into my code.
The incorporated code works and the download is successful when I download the file that the author has in the example. However when I change to load a file from my web site the download starts but soon fails with an error 2038. It seems to me that there is something that prevents the download on my site. Here is the code.
Code:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" verticalAlign="middle" backgroundColor="white" creationComplete="init();" viewSourceURL="srcview/index.html">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.collections.ArrayCollection;
import flash.net.FileReference;
[Bindable]
private var arrColl:ArrayCollection;
// URL of the file to download.
private const FILE_URL:String = "http://localhost/alma1208/public_html/Download/FileReference_download_test.zip";
private var fileRef:FileReference;
private var urlReq:URLRequest;
private function init():void {
// Initialize the array collection to an empty collection.
arrColl = new ArrayCollection();
// Set up the URL request to download the file specified by the FILE_URL variable
urlReq = new URLRequest(FILE_URL);
// Define file reference object and add a bunch of event listeners.
fileRef = new FileReference();
fileRef.addEventListener(Event.CANCEL, doEvent);
fileRef.addEventListener(Event.COMPLETE, doEvent);
fileRef.addEventListener(Event.OPEN, doEvent);
fileRef.addEventListener(Event.SELECT, doEvent);
fileRef.addEventListener(HTTPStatusEvent.HTTP_STATUS, doEvent);
fileRef.addEventListener(IOErrorEvent.IO_ERROR, doEvent);
fileRef.addEventListener(ProgressEvent.PROGRESS, doEvent);
fileRef.addEventListener(SecurityErrorEvent.SECURITY_ERROR, doEvent);
}
private function doEvent(evt:Event):void {
// Create shortcut to the FileReference object.
var fr:FileReference = evt.currentTarget as FileReference;
// Add event order and type to the DataGrid control.
arrColl.addItem({data:arrColl.length+1, type:evt.type, eventString:evt.toString()});
try {
// Update the Model.
fileRefModel.creationDate = fr.creationDate;
fileRefModel.creator = fr.creator;
fileRefModel.modificationDate = fr.modificationDate;
fileRefModel.name = fr.name;
fileRefModel.size = fr.size;
fileRefModel.type = fr.type;
// Display the Text control.
txt.visible = true;
} catch (err:*) {
// uh oh, an error of sorts.
}
}
private function downloadSourceCodeZip():void {
// Clear existing array collection.
arrColl = new ArrayCollection();
// Hide the Text control.
txt.visible = false;
// Begin download.
fileRef.download(urlReq);
}
private function showAlert(item:Object):void {
Alert.show(item.eventString, item.type);
}
]]>
</mx:Script>
<mx:Model id="fileRefModel">
<file>
<creationDate>{""}</creationDate>
<creator>{""}</creator>
<modificationDate>{""}</modificationDate>
<name>{""}</name>
<size>{""}</size>
<type>{""}</type>
</file>
</mx:Model>
<mx:Button id="downloadBtn" label="Download example source code" click="downloadSourceCodeZip()" toolTip="{FILE_URL}" height="40" />
<mx:DataGrid id="debug" dataProvider="{arrColl}" width="{downloadBtn.width}" rowCount="5" rowHeight="22" itemClick="showAlert(event.currentTarget.selectedItem)">
<mx:columns>
<mx:DataGridColumn dataField="data" headerText="#" width="20" />
<mx:DataGridColumn dataField="type" headerText="Type" showDataTips="true" dataTipField="eventString" />
</mx:columns>
</mx:DataGrid>
<mx:Text id="txt" condenseWhite="true" visible="false">
<mx:text>
creationDate: {fileRefModel.creationDate}
creator: {fileRefModel.creator}
modificationDate: {fileRefModel.modificationDate}
name: {fileRefModel.name}
size: {fileRefModel.size}
type: {fileRefModel.type}
</mx:text>
</mx:Text>
</mx:Application>