Uploads of files with Flash 8

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.

ActionScript

  1. import flash.net.FileReference;

Creating an object listener to use the methods onSelect, onOpen, onComplete, onHTTPError, onIOError, onSecurityError, onProgress.

ActionScript

  1. var listener:Object = new Object();
  2. listener.onSelect = function(selectedFile:FileReference):Void  {
  3.     statusArea.text += "Attempting to upload "+selectedFile.name+"\n";
  4.     selectedFile.upload("http://dominio/upload.php");
  5. };
  6. listener.onOpen = function(selectedFile:FileReference):Void  {
  7.     statusArea.text += "Opening "+selectedFile.name+"\n";
  8. };
  9. listener.onComplete = function(selectedFile:FileReference):Void  {
  10.     statusArea.text += "Downloading "+selectedFile.name+" to player\n";
  11.     load_img.load();
  12.     load_img.contentPath = "http://dominio/img/"+selectedFile.name;
  13. }
  14. ;listener.onHTTPError = function(selectedFile:FileReference):Void  {
  15.     statusArea.text += "onHTTPError: "+selectedFile.name;
  16. };
  17. listener.onIOError = function(selectedFile:FileReference):Void  {
  18.     statusArea.text += "onIOError: "+selectedFile.name;
  19. };
  20. listener.onSecurityError = function(selectedFile:FileReference, errorString:String):Void  {
  21.     statusArea.text += "onSecurityError: "+selectedFile.name+" errorString: "+errorString;
  22. };
  23. listener.onProgress = function(selectedFile:FileReference, bytesLoaded:Number, bytesTotal:Number):Void  {
  24.     statusArea.text += "onProgress with bytesLoaded: "+bytesLoaded+" bytesTotal: "+bytesTotal;
  25. };

Creating an instance of the FileReference class

ActionScript

  1. var imageFile:FileReference = new FileReference();

Adding to our listeners the bred instance.

ActionScript

  1. imageFile.addListener(listener);

Creating the button, the textarea and loader, dinamicamente necessary that these components are in the library of the film.

ActionScript

  1. var bot = _root.createClassObject(mx.controls.Button, "bot", _root.getNextHighestDepth(), {label:"Upload", _x:225});
  2. bot.onRelease = function() {
  3.     imageFile.browse([{description:"Image Files", extension:"*.jpg;*.gif;*.png;"}]);
  4. };
  5. var load_img = _root.createClassObject(mx.controls.Loader, "load_img", _root.getNextHighestDepth(), {_x:50, _y:22,_width:450, _height:270});
  6. load_img.setStyle("backgroundColor", 0xEEEEEE);
  7. var statusArea = _root.createClassObject(mx.controls.TextArea, "statusArea", _root.getNextHighestDepth(), {_x:1, _y:300, _width:549, _height:100});

see that in method browse, we can define which extensions will be allowed for upload.

ActionScript

  1. imageFile.browse([{description:"Image Files", extension:"*.jpg;*.gif;*.png;"}]);

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

ActionScript

  1. < ?php
  2. $dir = 'img';
  3. //path name of file for storage
  4. $uploadfile = "$dir/" . basename( $_FILES&#91;'Filedata']['name'] );
  5. //if the file is moved successfully
  6. if ( move_uploaded_file( $_FILES&#91;'Filedata']['tmp_name'] , $uploadfile ) ) {
  7.   echo( '1 ' . $_FILES&#91;'Filedata']['name']);
  8.  //file failed to move
  9. }else{
  10.   echo( '0');
  11. }?>

The complete Code in Action Script:

ActionScript

  1. import flash.net.FileReference;
  2. var listener:Object = new Object();
  3. listener.onSelect = function(selectedFile:FileReference):Void  {
  4.     statusArea.text += "Attempting to upload "+selectedFile.name+"\n";
  5.     selectedFile.upload("http://www.osfederais.com/upload.php");
  6. };
  7. listener.onOpen = function(selectedFile:FileReference):Void  {
  8.     statusArea.text += "Opening "+selectedFile.name+"\n";
  9. };
  10. listener.onComplete = function(selectedFile:FileReference):Void  {
  11.     statusArea.text += "Downloading "+selectedFile.name+" to player\n";
  12.     load_img.load();
  13.     load_img.contentPath = "http://www.osfederais.com/img/"+selectedFile.name;
  14.     trace("http://www.osfederais.com/img/"+selectedFile.name);
  15. };
  16. listener.onHTTPError = function(selectedFile:FileReference):Void  {
  17.     statusArea.text += "onHTTPError: "+selectedFile.name;
  18. };
  19. listener.onIOError = function(selectedFile:FileReference):Void  {
  20.     statusArea.text += "onIOError: "+selectedFile.name;
  21. };
  22. listener.onSecurityError = function(selectedFile:FileReference, errorString:String):Void  {
  23.     statusArea.text += "onSecurityError: "+selectedFile.name+" errorString: "+errorString;
  24. };
  25. listener.onProgress = function(selectedFile:FileReference, bytesLoaded:Number, bytesTotal:Number):Void  {
  26.     statusArea.text += "onProgress with bytesLoaded: "+bytesLoaded+"\n bytesTotal: "+bytesTotal+"\n";
  27. };
  28. var imageFile:FileReference = new FileReference();
  29. imageFile.addListener(listener);
  30. var bot = _root.createClassObject(mx.controls.Button, "bot", _root.getNextHighestDepth(), {label:"Upload", _x:225});
  31. bot.onRelease = function() {
  32.     imageFile.browse([{description:"Image Files", extension:"*.jpg;*.gif;*.png;"}]);
  33. };
  34. var load_img = _root.createClassObject(mx.controls.Loader, "load_img", _root.getNextHighestDepth(), {_x:50, _y:22,_width:450, _height:270});
  35. load_img.setStyle("backgroundColor", 0xEEEEEE);
  36. var statusArea = _root.createClassObject(mx.controls.TextArea, "statusArea", _root.getNextHighestDepth(), {_x:1, _y:300, _width:549, _height:100});

Clique aqui para testar você mesmo

Flash, Flash Platform, Principal, RIA

Leave a Reply