To make uploads of archives for the Flash was something that I needed to make for a work but at the time the Flash did not give support to this, had that to make with Javascript, but now together the new class that had been enclosed in the Flash 8is one that I come needing to some time, the FileReference class, this class already existed in Macromedia Central, exactly to make uploads of files with the Flash, will see as to implement this resource. It opens a new archive, in the first layer, presses F9 to open the panel “Actions�?. We go imports for swf the classr necessary to make upload of files.
[as]import flash.net.FileReference;[/as]
Creating an object listener to use the methods onSelect, onOpen, onComplete, onHTTPError, onIOError, onSecurityError, onProgress.
[as]var listener:Object = new Object();
listener.onSelect = function(selectedFile:FileReference):Void {
statusArea.text += “Attempting to upload “+selectedFile.name+”\n”;
selectedFile.upload(“http://dominio/upload.php”);
};
listener.onOpen = function(selectedFile:FileReference):Void {
statusArea.text += “Opening “+selectedFile.name+”\n”;
};
listener.onComplete = function(selectedFile:FileReference):Void {
statusArea.text += “Downloading “+selectedFile.name+” to player\n”;
load_img.load();
load_img.contentPath = “http://dominio/img/”+selectedFile.name;
}
;listener.onHTTPError = function(selectedFile:FileReference):Void {
statusArea.text += “onHTTPError: “+selectedFile.name;
};
listener.onIOError = function(selectedFile:FileReference):Void {
statusArea.text += “onIOError: “+selectedFile.name;
};
listener.onSecurityError = function(selectedFile:FileReference, errorString:String):Void {
statusArea.text += “onSecurityError: “+selectedFile.name+” errorString: “+errorString;
};
listener.onProgress = function(selectedFile:FileReference, bytesLoaded:Number, bytesTotal:Number):Void {
statusArea.text += “onProgress with bytesLoaded: “+bytesLoaded+” bytesTotal: “+bytesTotal;
};[/as]
Creating an instance of the FileReference class
[as]var imageFile:FileReference = new FileReference();[/as]
Adding to our listeners the bred instance.
[as]imageFile.addListener(listener);[/as]
Creating the button, the textarea and loader, dinamicamente necessary that these components are in the library of the film.
[as]var bot = _root.createClassObject(mx.controls.Button, “bot”, _root.getNextHighestDepth(), {label:”Upload”, _x:225});
bot.onRelease = function() {
imageFile.browse([{description:”Image Files”, extension:”*.jpg;*.gif;*.png;”}]);
};
var load_img = _root.createClassObject(mx.controls.Loader, “load_img”, _root.getNextHighestDepth(), {_x:50, _y:22,_width:450, _height:270});
load_img.setStyle(“backgroundColor”, 0xEEEEEE);
var statusArea = _root.createClassObject(mx.controls.TextArea, “statusArea”, _root.getNextHighestDepth(), {_x:1, _y:300, _width:549, _height:100});[/as]
see that in method browse, we can define which extensions will be allowed for upload.
[as]imageFile.browse([{description:”Image Files”, extension:”*.jpg;*.gif;*.png;”}]);[/as]
A window of explorer will confide the button as soon as will be clicado. The archive for upload will be sent for ours script in PHP that finishes to order the archive for the server. Script using was the following one:
Arquivo: upload.php
[as]< ?php
$dir = 'img';
//path name of file for storage
$uploadfile = "$dir/" . basename( $_FILES['Filedata']['name'] );
//if the file is moved successfully
if ( move_uploaded_file( $_FILES['Filedata']['tmp_name'] , $uploadfile ) ) {
echo( '1 ' . $_FILES['Filedata']['name']);
//file failed to move
}else{
echo( '0');
}?>[/as]
The complete Code in Action Script:
[as]import flash.net.FileReference;
var listener:Object = new Object();
listener.onSelect = function(selectedFile:FileReference):Void {
statusArea.text += “Attempting to upload “+selectedFile.name+”\n”;
selectedFile.upload(“http://www.osfederais.com/upload.php”);
};
listener.onOpen = function(selectedFile:FileReference):Void {
statusArea.text += “Opening “+selectedFile.name+”\n”;
};
listener.onComplete = function(selectedFile:FileReference):Void {
statusArea.text += “Downloading “+selectedFile.name+” to player\n”;
load_img.load();
load_img.contentPath = “http://www.osfederais.com/img/”+selectedFile.name;
trace(“http://www.osfederais.com/img/”+selectedFile.name);
};
listener.onHTTPError = function(selectedFile:FileReference):Void {
statusArea.text += “onHTTPError: “+selectedFile.name;
};
listener.onIOError = function(selectedFile:FileReference):Void {
statusArea.text += “onIOError: “+selectedFile.name;
};
listener.onSecurityError = function(selectedFile:FileReference, errorString:String):Void {
statusArea.text += “onSecurityError: “+selectedFile.name+” errorString: “+errorString;
};
listener.onProgress = function(selectedFile:FileReference, bytesLoaded:Number, bytesTotal:Number):Void {
statusArea.text += “onProgress with bytesLoaded: “+bytesLoaded+”\n bytesTotal: “+bytesTotal+”\n”;
};
var imageFile:FileReference = new FileReference();
imageFile.addListener(listener);
var bot = _root.createClassObject(mx.controls.Button, “bot”, _root.getNextHighestDepth(), {label:”Upload”, _x:225});
bot.onRelease = function() {
imageFile.browse([{description:”Image Files”, extension:”*.jpg;*.gif;*.png;”}]);
};
var load_img = _root.createClassObject(mx.controls.Loader, “load_img”, _root.getNextHighestDepth(), {_x:50, _y:22,_width:450, _height:270});
load_img.setStyle(“backgroundColor”, 0xEEEEEE);
var statusArea = _root.createClassObject(mx.controls.TextArea, “statusArea”, _root.getNextHighestDepth(), {_x:1, _y:300, _width:549, _height:100});[/as]
Clique aqui para testar você mesmo