hey guys, i found a few scripts online and combined them to this. I want to download files from the web to my local harddrive. Any idea what i'm doing wrong?
var fs:FileStream;
var stream:URLStream;
var _output:Boolean = false;
init();
startDownload('http://www.teachenglishinasia.net/files/u2/purple_lotus_flower.jpg');
function init() {
stream = new URLStream();
stream.addEventListener(ProgressEvent.PROGRESS, _dlProgressHandler);
stream.addEventListener(Event.COMPLETE, _dlCompleteHandler);
stream.addEventListener(Event.OPEN, _dlStartHandler);
fs = new FileStream();
fs.addEventListener(OutputProgressEvent.OUTPUT_PROGRESS, _writeProgressHandler)
}
function startDownload(url:String):void {
//fs.openAsync(lfile, FileMode.APPEND);
_output = false;
stream.load(new URLRequest(url));
}
function downloadComplete():void {
var fileData:ByteArray = new ByteArray();
stream.readBytes(fileData,0,stream.bytesAvailable);
fs.writeBytes(fileData,0,fileData.length);
fs.close();
}
function writeToDisk():void {
_output = false;
var fileData:ByteArray = new ByteArray();
stream.readBytes(fileData,0,stream.bytesAvailable);
fs.writeBytes(fileData,0,fileData.length);
}
function _dlProgressHandler(evt:ProgressEvent):void {
if(_output){
writeToDisk();
}
}
function _dlCompleteHandler(evt:Event):void {
downloadComplete();
}
function _dlStartHandler(evt:Event):void {
_output = true;
}
function _writeProgressHandler(evt:OutputProgressEvent):void{
_output = true;
}
Flash keeps telling me: Error: Error #2029: This URLStream object does not have a stream opened. However the connection to the webpage goes out.
Any ideas? Thank you for your help!