Combined with Asp.net upload, flex provides an in-depth explanation

Source: Internet
Author: User
Tags php example

Combined with Asp.net upload, flex provides an in-depth explanation
Flex/flash, flex2, flex3 ggnet January 23 rd. 2008, AM
Flex (or as3.0, the same below) can be found by searching online based on examples uploaded by ASP. NET. But in ASP. NET, how does one know the data transmitted by Flex? There is a form tag in the HTML page. Generally, the form tag contains code such as <input id = "myfile" type = "file" runat = "server"/>, with that ID, we use myfile. postedfile. contentlength (size of the uploaded file, in bytes), myfile. postedfile. filename (full path of the File Uploaded by the client )...... Because myfile. postedfile is already an httppostedfile object. However, if you submit it to ASP. NET using Flex, how can you obtain and use the submitted httppostedfile object? Based on the official explanation and the PHP example, I have a clue at last. If you have understood it, please give me more instructions after reading this article ^ _ ^. My blog is www.riahome.cn.
(This article assumes that you have a certain degree of foundation for flex and ASP. NET, now only the core code to explain, the following has a complete source file download) [my blog-http://www.RiaHome.cn]
Suppose we have created a new filereference object (Private var file: filereference = new filereference ();) and added a listener (for example, a listener event) to the filereference object. select, ioerrorevent. io_error events), so after you have selected the file to be uploaded, what we need to do is upload it to the remote server-side script (here is ASP. net), then we execute: file. upload (New URLRequest ("upfile. aspx); by default, flex will use the POST method to upload.
An http post request example provided in the official document (please note the highlights of different colors ):
Post/handler. asp HTTP/1.1
Accept: text /*
Content-Type: multipart/form-data;
Boundary = ---- ij5ae0ae0km7gi3km7ei4ch2ei4gl6
User-Agent: Shockwave Flash
HOST: www.mydomain.com
Content-Length: 421
Connection: keep-alive
Cache-control: No-Cache
---- Ij5ae0ae0km7gi3km7ei4ch2ei4gl6
Content-Disposition: Form-data; name = "filename"
Sushi.jpg
---- Ij5ae0ae0km7gi3km7ei4ch2ei4gl6
Content-Disposition: Form-data; name = "filedata"; filenameappssushi.jpg"
Content-Type: Application/octet-stream
Test File
---- Ij5ae0ae0km7gi3km7ei4ch2ei4gl6
Content-Disposition: Form-data; name = "upload"
Submit Query
---- Ij5ae0ae0km7gi3km7ei4ch2ei4gl6
(Actual file data ,,,)
In the above http post request example, you will find the name = "filedata"; filename=”sushi.jpg. filedata is system. web. httppostedfile object. The filename variable is the name of the uploaded file. net ). if we can obtain the system. web. httppostedfile object, we can know the size (contentlength attribute), type (contenttype attribute), full path (filename attribute) of the uploaded file on the client, and so on. in ASP. how to obtain and use filedata in. Net?
See the ASP. NET code found on the Internet (please pay attention to the highlighted part ):
<Script language = "C #" runat = "server">
String uploadfolder = "uploadedfiles"; // folder on the server where the uploaded files are stored
Private void page_load (Object sender, system. eventargs e ){
/*
The following is the system. Web. httpfilecollection file submitted by Flex.
Object. Why is it "request. Files? Or what? I have read the PHP files. I don't know why. Hope you can give me some advice!
*/
Httpfilecollection files = request. files;
If (files. Count = 0 ){
Response. Write ("do not directly access this file");
Response. End ();
}
String Path = server. mappath (uploadfolder );
// Get only 1st files
Httppostedfile file = files [0];
If (file! = NULL & file. contentlength> 0 ){
// Request. Form ["FILENAME"] indicates the name of the uploaded file.
String savepath = path + "/" + request. Form ["FILENAME"];
File. saveas (savepath );
}
/*
The following is an additional one I added and used for testing. The output data below can be used with filereference.
Dataevent. upload_complete_data
The event is obtained. You can download the source file and test it by yourself.
*/
Response. Write (request. Form ["FILENAME"]);
Response. Write (request. files );
Response. Write (request. Files ["filedata"]);
Response. Write (request. Files ["filedata"]. contentlength );
}
</SCRIPT>
Explain the above four sentences respectively: Response. Write ()
Statement:
Response. Write (request. Form ["FILENAME"]);
The first sentence will output the name of the uploaded file, for example, "myphoto.jpg".
Response. Write (request. files );
The second sentence will output: "system. Web. httpfilecollection". It indicates that request. Files is an httpfilecollection object submitted by Flex (or as3). It contains multiple httppostedfile objects.
Response. Write (request. Files ["filedata"]);
The third sentence will output: "system. web. httppostedfile ". because the request. files is an httpfilecollection object that contains multiple httppostedfile objects. files contains an httppostedfile object, which can be obtained as follows: request. files [0], or get: request. files ["filedata"].
Do you still remember the statements uploaded in as3.0? That is: file. Upload (New URLRequest ("upfile. aspx"); the complete use of the upload () method of the filereference object is:
Upload (request: URLRequest, uploaddatafieldname: String = "filedata", testupload: Boolean = ALSE ):
Let's take a look at the second parameter. Because the default value is "filedata", here is: request. files ["filedata"]. if you customize the second parameter (for example, "mydata"), you must obtain the request in this way. files ["mydata"].
Response. Write (request. Files ["filedata"]. contentlength );
The fourth sentence will output the size of the uploaded file. The third sentence above said request. Files ["filedata"] is an httppostedfile
Object, so it contains some attributes: the size of the uploaded file (contentlength attribute), type (contenttype attribute), full path of the uploaded file on the client (filename attribute), and so on.
Through the above test, if you only upload one file, the ASP. NET file can also be written as follows:
<Script language = "C #" runat = "server">
String uploadfolder = "uploadedfiles ";
Private void page_load (Object sender, system. eventargs e ){
String Path = server. mappath (uploadfolder );
Httppostedfile file = request. Files ["filedata"];
If (file! = NULL & file. contentlength> 0 ){
String savepath = path + "/" + request. Form ["FILENAME"];
File. saveas (savepath );
}
}
</SCRIPT>

Reprinted from: http://www.RiaHome.cn

Source File Download: Compressed package download

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.