Flex2.0 implements the file upload function (the server is ASP. NET)

Source: Internet
Author: User
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 );
}
}

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.