测试平台:flash8+Asp2.0
flash8中使用了flash.net.FileReference文件上传类.
部分flash8客户端代码:
System.security.allowDomain("*");
import flash.net.FileReference;
// The listener object listens for FileReference events.
var listener:Object = new Object();
// When the user selects a file, the onSelect() method is called, and
// passed a reference to the FileReference object.
var filename;
var path = "http://szsoho.com/flashupload/";
System.useCodepage = true;
listener.onSelect = function(selectedFile:FileReference):Void {
// Update the TextArea to notify the user that Flash is attempting to
// upload the image.
statusArea.text += "Attempting to upload "+selectedFile.name+"\n";
// Upload the file to the ASP/PHP/ASPX script on the server.
var indexid = selectedFile.name.indexOf(".", 0);
filename = selectedFile.name.substr(0, indexid)+int(Math.random()*100000)+"."+selectedFile.name.substr(indexid+1, selectedFile.name.length-indexid);
if (!selectedFile.upload("http://221.237.164.185/flashupload/attachment.asp?filename="+filename)) {
trace("Upload dialog failed to open.");
}
};
// When the file begins to upload the onOpen() method is called, so
// notify the user that the file is starting to upload.
listener.onOpen = function(selectedFile:FileReference):Void {
statusArea.text += "Opening "+selectedFile.name+"\n";
};
// Once the file has uploaded, the onComplete() method is called.
listener.onComplete = function(selectedFile:FileReference):Void {
// Notify the user that Flash is starting to download the image.
statusArea.text += "Downloading "+filename+" to player\n";
// Add the image to the ComboBox component.
imagesCb.addItem(filename, path+"up/"+filename);
// Set the selected index of the ComboBox to that of the most recently-
// added image.
imagesCb.selectedIndex = imagesCb.length-1;
// Call the custom downloadImage() function.
downloadImage();
};
listener.onProgress = function(file:FileReference, bytesLoaded:Number, bytesTotal:Number):Void {
statusArea.text += "onProgress with bytesLoaded: "+bytesLoaded+" bytesTotal: "+bytesTotal;
};
listener.onHTTPError = function(file:FileReference, httpError:Number):Void {
statusArea.text += "onHTTPError: "+file.name;
statusArea.text += "onHTTPErrorNumber:"+httpError;
};
listener.onIOError = function(file:FileReference):Void {
statusArea.text += "onIOError: "+file.name;
};
listener.onSecurityError = function(file:FileReference, errorString:String):Void {
statusArea.text += "onSecurityError: "+file.name+" errorString: "+errorString;
};
var imageFile:FileReference = new FileReference();
imageFile.addListener(listener);
uploadBtn.addEventListener("click", uploadImage);
// When the user clicks the button Flash calls the uploadImage() function,
// and it opens a file browser dialog.
function uploadImage(event:Object):Void {
//imageFile.browse([{description:"Image Files", extension:"*.jpg;*.gif;*.png"}]);
imageFile.browse([{description:"Image Files", extension:"*"}]);
}
本范例适合将数据提交给ASP,PHP,或ASPX服务器上。
在服务器端放如下几个文件:
attachment.asp:获取数据,并保存文件
upfile.asp:无组件上传类
readlist.asp:读取服务器文件数据
crossdomain.xml:跨域策略文件
attachment.asp
<!--#include file="upfile.asp" -->
<%
on error resume next
'必须先接受request变量
Dim up1
filename=request("filename")
Dim n1,n2,n3,n4,n5,n6,nt
Dim F_File,F_Type
Dim FileUP
Set FileUP=New Upload_File
Set F_File=FileUP.File("filedata")
'服务器名称保存
'Randomize
'n1=int(rnd*9)+1
'n2=int(rnd*9)+1
'n3=int(rnd*9)+1
'n4=int(rnd*9)+1
'n5=int(rnd*9)+1
'n6=int(rnd*9)+1
'nt=cstr(n1) & cstr(n2) & cstr(n3) & cstr(n4) & cstr(n5) & cstr(n6)
'F_Name=cstr(date) &cstr(nt)&"."&F_File.FileExt
'使用客户端名称保存
F_File.SaveAs Server.MapPath("up/"&filename)
Set F_File=Nothing
Set FileUP=Nothing
%>
upfile.asp 这里就不在放上,它将自动获取表单数据
'====== Upload_5xSoft Class ====================================
' 化境ASP无组件上传类 2.0
' 版权归原作者所有。
' 舜子于 2005-10-20 修改
'============================================================
readlist.asp
<%@ Language=VBScript %>
<%
Response.Expires=-1
dim fs,path,fname,num,i,finish,mnum
i=1
set fs=createobject("scripting.filesystemobject")
set path=fs.getfolder(server.mappath("up"))
set fname=path.files
'文件夹遍历
for each num in fname
if num.name<>"Thumbs.db" then
response.write "&mp3name"&i&"="&mid(num.name,1,len(num.name)-4)
response.write "&mp3addr"&i&"=up/"&num.name
i=i+1
end if
next
response.write ("&totalnum="&i-1)
set fs=nothing
%>
crossdomain.xml
<?xml version="1.0" encoding="iso-8859-1" ?>
<!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">
<!-- http://www.helpexamples.com/crossdomain.xml -->
<cross-domain-policy>
<allow-access-from domain="*" />
</cross-domain-policy>