3

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

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

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

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

拜托,请帮忙,我对 Flash 的限制,只学习了 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";
}
?>
4

1 回答 1

0

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

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

您需要使用 FileReferenceList 而不是 FileReference。

于 2012-10-09T10:02:53.293 回答