Flash AS3 FileReference - select and download multiple files one at a time

I currently have swf that allows you to select and display a file and upload it to the server using FileReference. This works fine, but I need to be able to select and display several (up to 25 in some cases), and then load them all at the end.

I know that you can use FileReferenceList to simultaneously select multiple files in a popup dialog, but my problem is that the user needs to select one at a time, make material for this image, then choose another, do things and so on ... then at the end, when they click on download, upload them all to the server.

Is this possible or is there a way, so I can add each new selected file to the array, and then at the end it loads all the files in the array either in one pass or through each loop, until all the objects in the array are full?

Can anyone help? I will send the complete code for my working download of a single file below.

Please help me, I am limited to flash memory and studied only 3-4 months, and I’ve been stuck for more than a week :(

Lauren

as3 Code:

import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.net.FileReference;
import flash.net.FileFilter;
import flash.utils.ByteArray;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.display.MovieClip;
import fl.controls.ProgressBarMode;
import flash.display.Bitmap;
import flash.display.BitmapData;

progressBar.visible=false;
UploadprogressBar.visible=false;

// Create FileReference.
var imageFile:FileReference;

// Create Loader to hold image content
var image_loader:Loader = new Loader();
var image_content:Sprite = new Sprite();

// Get Extension Function.
var imageExtension;

function getExtension($url:String):String {
    var extension:String = $url.substring($url.lastIndexOf(".")+1, $url.length);
    return extension;
}

// Random Number Function to create new filename on server.
function randomNum(low:Number=0, high:Number=1):Number {
return Math.floor(Math.random() * (1+high-low)) + low;
}
var myNumber:Number= randomNum(1000,9999);
var myString:String= String(myNumber);
var RandomNumbers = myString;
var imageFilePath = (RandomNumbers);

// Select Button Function.
select_btn.addEventListener(MouseEvent.CLICK, onselect_btnClicked);

function onselect_btnClicked(event:MouseEvent):void {
imageFile=new FileReference();
imageFile.addEventListener(Event.SELECT, onFileSelected);
var imageTypeFilter:FileFilter = new FileFilter("JPG/PNG Files","*.jpeg; *.jpg;*.gif;*.png");
imageFile.browse([imageTypeFilter]);
}

// File Selected Function.
function onFileSelected(event:Event):void {
imageFile.addEventListener(Event.COMPLETE, onFileLoaded);
imageFile.addEventListener(ProgressEvent.PROGRESS, onProgress);
var imageFileName = imageFile.name;
imageExtension = getExtension(imageFileName);
imageFile.load();

progressBar.visible=true;
progressBar.mode=ProgressBarMode.MANUAL;
progressBar.minimum=0;
progressBar.maximum=100;
UploadprogressBar.mode=ProgressBarMode.MANUAL;
UploadprogressBar.minimum=0;
UploadprogressBar.maximum=100;
}

// File Progress Function.
function onProgress(event:ProgressEvent):void {
var percentLoaded:Number=event.bytesLoaded/event.bytesTotal*100;
progressBar.setProgress(percentLoaded, 100);
}

// File Loaded Function.
function onFileLoaded(event:Event):void {
var fileReference:FileReference=event.target as FileReference;

var data:ByteArray=fileReference["data"];
var movieClipLoader:Loader=new Loader();
movieClipLoader.loadBytes(data);
movieClipLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onMovieClipLoaderComplete);

imageFile.removeEventListener(Event.COMPLETE, onFileLoaded);
imageFile.removeEventListener(ProgressEvent.PROGRESS, onProgress);
}

// Load Image onto Stage Function.
function onMovieClipLoaderComplete(event:Event):void {
var loadedContent:DisplayObject=event.target.content;
image_loader =event.target.loader as Loader;

var scaleWidth:Number=345/image_loader.width;
image_loader.scaleX=image_loader.scaleY=scaleWidth;
image_loader.height=200;
image_loader.scaleX=image_loader.scaleY;
image_loader.x=10;
image_loader.y=10;

image_content.buttonMode=true;
image_content.addChild(image_loader);
addChild(image_content);
}

// Upload Button Function.
upload_btn.addEventListener(MouseEvent.CLICK, onupload_btnClicked);

function onupload_btnClicked(event:MouseEvent):void {
var filename:String=imageFile.name;
var urlRequest:URLRequest = new URLRequest("http://www.xxxxx.com/xxxxx/file-reference.php");
var variables:URLVariables = new URLVariables();
urlRequest.method = URLRequestMethod.POST;
imageFile.addEventListener(ProgressEvent.PROGRESS, onUploadProgress);
imageFile.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA,onUploadComplete);
variables.ID = (RandomNumbers);
urlRequest.data = variables;
imageFile.upload(urlRequest);
}

// File Upload Progress Function.
function onUploadProgress(event:ProgressEvent):void {
var percentLoaded:Number=event.bytesLoaded/event.bytesTotal*100;
UploadprogressBar.setProgress(percentLoaded, 100);
trace("loaded: "+percentLoaded+"%");
upload_status_txt.text='upload in progress...';
}

// Upload File Completed Function.
function onUploadComplete(event:Event):void {
upload_status_txt.text='upload complete';
imageFile.removeEventListener(ProgressEvent.PROGRESS, onProgress);
imageFile.removeEventListener(DataEvent.UPLOAD_COMPLETE_DATA,onUploadComplete);
UploadprogressBar.visible=false;
}

php server code:

<?php
function getExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
    $l = strlen($str) - $i;
    $ext = substr($str,$i+1,$l);
    return $ext;
}

$fileID = $_POST['ID'];
$filename = basename( $_FILES['Filedata']['name']);

$extension = getExtension($filename);
$extension = strtolower($extension);
$uploadID = $fileID.'.'.$extension;
if (move_uploaded_file($_FILES['Filedata']['tmp_name'], "images/".$uploadID))
{
     echo "OK";
}
else
{
     echo "ERROR";
}
?>
+3
source share
1 answer

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/FileReference.html

"When you call the FileReferenceList.browse () method, which creates an array of FileReference objects."

You need to use FileReferenceList instead of FileReference.

0
source

All Articles