Introduction:
The new flex2.0 Class Library provides file classes to facilitate file upload/download. The program demo below demonstrates how to generate flash from flex2.0 to access local files, upload the selected files to the server in flash, and the flash client can process multiple events such as file upload progress, the server is a C # file receiving module that stores user-uploaded files on the server.
Demo demonstrates how to handle two events: progresseventtype. Progress and eventtype. Select.
Test Results:
Test Environment:
Operating System: Windows2003 Server
Flex version: Flex 2.0 alpha 1
Flash: Flash Player 8.5
Web Server:
In IIS 6.0
. NET Framework 1.1
Client code: Fileupload. mxml
<? XML version = "1.0" encoding = "UTF-8"?>
<Mx: Application xmlns: MX = "http://www.macromedia.com/2005/mxml"
Xmlns = "*" creationcomplete = "Init ();">
<Mx: SCRIPT>
<! [CDATA [
Import flash.net. filereference;
Import MX. Controls. Alert;
Import MX. Events. alertclickevent;
Import flash. Events .*;
Var file: filereference;
Private function Init (){
Security. allowdomain ("*");
File = new filereference ();
File. addeventlistener (progresseventtype. Progress, onprogress );
File. addeventlistener (eventtype. Select, onselect );
}
Private function upload (){
File. Browse ();
}
Private function onselect (E: Event ){
Alert. Show ("Upload" + file. Name + "(total" + math. Round (file. Size) + "bytes )? ",
"Confirm upload ",
Alert. Yes | alert. No,
Null,
Proceedwithupload );
}
Private function onprogress (E: progressevent ){
Lbprogress. Text = "uploaded" + E. bytesloaded
+ "Bytes, total" + E. bytestotal + "bytes ";
}
Private function proceedwithupload (E: alertclickevent ){
If (E. Detail = alert. Yes ){
File. Upload ("http: // localhost/jzservice/webform1.aspx ");
}
}
]>
</MX: SCRIPT>
<Mx: canvas width = "100%" Height = "100%">
<Mx: vbox width = "100%" horizontalalign = "center">
<Mx: Label id = "lbprogress" text = "Upload"/>
<Mx: button label = "Upload File" Click = "Upload ();"/>
</MX: vbox>
</MX: canvas>
</MX: Application>
Server code: Webform1.aspx
Private void page_load (Object sender, eventargs e ){
// Place user code here to initialize the page
Httpfilecollection uploadedfiles = request. files;
String Path = server. mappath ("data ");
For (INT I = 0; I <uploadedfiles. Count; I ++ ){
Httppostedfile F = uploadedfiles [I];
If (uploadedfiles [I]! = NULL & F. contentlength> 0 ){
String newname = f. filename. substring (F. filename. lastindexof ("\") + 1 );
F. saveas (path + "/" + newname );
}
}
}