Using Flash Builder 4 (with 4.1 flex) and found/modified a script that works with a single file without prompting. Now I need to modify it to download and save more than one file.
The originator of the code said to introduce an array, but don't know exactly how to. Ideally you push the button once and it initiates the function and then the function reads the array and runs through the commands with the variables defined from the first result of the array, then changes to next.
The values would only change on the name of the files not on the location, locally or remotely.I know how to do this in php but not flex. Anyone able to complete this?
<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" width="400" height="200"
title="Updater"
showStatusBar="false">
<fx:Style source="Main.css"/>
<fx:Script>
<![CDATA[
protected function download_clickHandler(event:MouseEvent):void
{
var remoteFile = "http://domain/01.jpg";
var localFile = "C:/01.jpg";
var urlStream = new URLStream();
var request = new URLRequest(remoteFile);
var fileStream = new FileStream();
var file = File.desktopDirectory.resolvePath(localFile);
var writeFile = function()
开发者_运维问答 {
// Write to file
if (urlStream.bytesAvailable > 51200)
{
var dataBuffer = new ByteArray();
urlStream.readBytes(dataBuffer, 0, urlStream.bytesAvailable);
fileStream.writeBytes(dataBuffer, 0, dataBuffer.length);
}
return true;
}
var finishWriteFile = function()
{
if(urlStream.bytesAvailable > 0)
{
var dataBuffer = new ByteArray();
urlStream.readBytes(dataBuffer, 0, urlStream.bytesAvailable);
fileStream.writeBytes(dataBuffer, 0, dataBuffer.length);
}
fileStream.close();
urlStream.close();
return true;
}
fileStream.openAsync(file, FileMode.WRITE);
urlStream.load(request);
urlStream.addEventListener(Event.COMPLETE, finishWriteFile);
urlStream.addEventListener(ProgressEvent.PROGRESS, writeFile);
}
}
]]>
</fx:Script>
<mx:Image x="0" y="0" source="background.jpg"/>
<s:Button x="85" y="85" label="Update" id="download" click="download_clickHandler(event)" color="#FFFFFF" fontSize="30"/></s:WindowedApplication>
Thanks for any input!
This is what I have now, and it works. Anyone see any issues with how it is done?
<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" width="400" height="200"
title=" Updater"
showStatusBar="false">
<fx:Style source="Main.css"/>
<fx:Script>
<![CDATA[
private function download_clickHandler(event:MouseEvent):void
{
var filearray:Array = new Array();
filearray[0]="pregame.jpg";
filearray[1]="01.jpg";
filearray[2]="02.jpg";
filearray[3]="03.jpg";
filearray[4]="04.jpg";
filearray[5]="05.jpg";
filearray[6]="06.jpg";
filearray[7]="07.jpg";
filearray[8]="08.jpg";
filearray[9]="09.jpg";
filearray[10]="10.jpg";
for (var i:uint; i < filearray.length; i++) {
var remoteURL = "http://www.domain.com/" + filearray[i];
var localURL = "C:/dir/" + filearray[i];
downloadFile(remoteURL, localURL);
}
function downloadFile(url, fileName) {
// Create the stream for the data request
var urlStream = new URLStream();
// Used to initiate request for remote file
var request = new URLRequest(url);
// Create file stream
var fileStream = new FileStream();
// Create a reference to a location on disk
var file = File.desktopDirectory.resolvePath(fileName);
// Called as download progresses
var writeFile = function()
{
// Write to file
if (urlStream.bytesAvailable > 51200)
{
var dataBuffer = new ByteArray();
urlStream.readBytes(dataBuffer, 0, urlStream.bytesAvailable);
fileStream.writeBytes(dataBuffer, 0, dataBuffer.length);
}
return true;
}
// Called when download completes
var finishWriteFile = function()
{
// Write to file
if(urlStream.bytesAvailable > 0)
{
var dataBuffer = new ByteArray();
urlStream.readBytes(dataBuffer, 0, urlStream.bytesAvailable);
fileStream.writeBytes(dataBuffer, 0, dataBuffer.length);
}
// Close streams
fileStream.close();
urlStream.close();
return true;
}
// Initiate download
fileStream.openAsync(file, FileMode.WRITE);
urlStream.load(request);
// Add event listeners
urlStream.addEventListener(Event.COMPLETE, finishWriteFile);
urlStream.addEventListener(ProgressEvent.PROGRESS, writeFile);
}
}
]]>
</fx:Script>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<mx:Image x="0" y="0" source="background.jpg"/>
<s:Button x="85" y="85" label="Update" id="download" click="download_clickHandler(event)" color="#FFFFFF" fontSize="30"/>
精彩评论