廢話少說,先看flash代碼,在flash中新建一個(gè)文檔,放上兩個(gè)button,一個(gè)progressbar組件:
import flash.net.FileReference;
import mx.controls.Alert;
var my_pb:mx.controls.ProgressBar;
var maxSize = 1000*1000*5; //文件大小限制:5M
//設(shè)置進(jìn)度欄模式
my_pb.mode = "manual";
my_pb.label = "上傳進(jìn)度:%1%";
//進(jìn)度欄增加前的最小數(shù)值
my_pb.minimum = 0;
// 進(jìn)度欄停止前的最大值
my_pb.maximum = 100;
var increment_num:Number = my_pb.minimum;
var allTypes:Array = new Array();
var imageTypes:Object = new Object();
imageTypes.description = "Images (*.jpg, *.jpeg, *.gif, *.png)";
imageTypes.extension = "*.jpg; *.jpeg; *.gif; *.png";
allTypes.push(imageTypes);
var textTypes:Object = new Object();
textTypes.description = "Text Files (*.txt, *.rtf)";
textTypes.extension = "*.txt;*.rtf";
allTypes.push(textTypes);
var flvType:Object =new Object();
flvType.description ="flv Files (*.flv)";
flvType.extension ="*.flv";
allTypes.push(flvType);
// 定義警告確認(rèn)后的動(dòng)作。
var myClickHandler:Function = function (evt_obj:Object) {
if (evt_obj.detail == Alert.OK) {
trace("start stock app");
}
};
var listener:Object = new Object();
listener.onSelect = function(file:FileReference):Void {
trace("onSelect: " + file.name);
//if(!file.upload("
http://localhost/upfiles/hlp.php")) {
// trace("Upload dialog failed to open.");
//}
if (file.size<=maxSize)
{
//strState.text = "您選擇得文件:" + file.name + "\n";
btnUpload.enabled = true;
}else
{
msg( "對(duì)不起您選擇的文件太大!");
btnUpload.enabled = false;
}
}
listener.onCancel = function(file:FileReference):Void {
trace("onCancel");
}
listener.onOpen = function(file:FileReference):Void {
trace("onOpen: " + file.name);
}
listener.onProgress = function(file:FileReference, bytesLoaded:Number, bytesTotal:Number):Void {
trace("onProgress with bytesLoaded: " + bytesLoaded + " bytesTotal: " + bytesTotal);
my_pb.setProgress ((bytesLoaded / bytesTotal) * 100, my_pb.maximum);
}
listener.onComplete = function(file:FileReference):Void {
trace("onComplete: " + file.name);
msg("上傳完畢!");
}
listener.onHTTPError = function(file:FileReference):Void {
trace("onHTTPError: " + file.name);
msg("onHTTPError: " + file.name);
}
listener.onIOError = function(file:FileReference):Void {
trace("onIOError: " + file.name);
msg("onIOError: " + file.name);
}
listener.onSecurityError = function(file:FileReference, errorString:String):Void {
trace("onSecurityError: " + file.name + " errorString: " + errorString);
msg("onSecurityError: " + file.name + " errorString: " + errorString);
}
var fileRef:FileReference = new FileReference();
fileRef.addListener(listener);
this.btnBrowse.onRelease = function () {
fileRef.browse(allTypes);
};
this.btnUpload.onRelease = function () {
if(!fileRef.upload("
http://localhost/upfiles/upfile3.php")) {
trace("Upload dialog failed to open.");
}
// 顯示警告對(duì)話框。
}
function msg(msgtxt:String) {
Alert.show(msgtxt, "消息", Alert.OK , this, myClickHandler, "stockIcon", Alert.OK);
}
php文件很簡(jiǎn)單:
<?php
// Flash 傳遞的文件表單 name 屬性為 Filedata
$fileName = $_FILES["Filedata"]["name"];
$file = $_FILES["Filedata"]["tmp_name"];
$path = "uploadFiles/";
if (move_uploaded_file($file, $path . $fileName)){
// echo 1;
}else{
// echo 0;
}
?>