Flash AS3 FileReference - 一次选择并上载多个文件

时间:2012-05-10 10:37:15

标签: actionscript-3 upload filereference

我目前有一个swf,允许您选择并显示一个文件,并使用FileReference将其上传到服务器。这很好但我需要能够选择和显示多个(在某些情况下最多25个),然后在最后上传它们。

我知道您可以使用FileReferenceList在对话框弹出窗口中同时选择多个文件,但我的问题是用户需要一次选择一个,对该图像执行操作,然后选择另一个,执行操作和所以...然后在他们按上传时,它们全部上传到服务器上。

是否有可能或是否有办法让我可以将每个新选择的文件添加到数组中,然后最后它将数据中的所有文件一次性上传或循环遍历每个文件直到数组中的所有对象完成了吗?

有人可以帮忙吗?我将在下面发布我的工作单文件上传的完整代码。

拜托,请帮助,我受限于闪光灯,只学习了3-4个月,而且我已经被困了一个多星期了:(

劳伦

as3代码:

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服务器代码:

<?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";
}
?>

1 个答案:

答案 0 :(得分:0)

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

“当您调用FileReferenceList.browse()方法时,它会创建一个FileReference对象数组。”

您需要使用FileReferenceList而不是FileReference。