Uploading and downloading files in flex

Source: Internet
Author: User

Key words: File Upload, file download, filereference, filereferencelist, filefilter

 

During project development, files are often uploaded and downloaded. However, due to the security sandbox in flex, the flex program cannot directly access local files. However, the flex SDK provides the filereference and filereferencelist classes, which can be implemented through these two classes, which are located in the flash.net package.
I. File Upload
Processing of uploaded files on the flex end:
1. upload a file. Step 1: open the file.
First, create a filereference object:
Private var myfilereference: filereference = new filereference ();
There is a browse () function in filereference. This function is used to open a Windows file selection box and select the file to be uploaded. In the open file selection box, you can specify the file type, which is achieved through filefilter. Create a filefilter object as a parameter of the browse function, so that you can specify the type of the selected file:
VaR imagesfilter: filefilter = new filefilter ("Images (*. jpg, *. GIF, *. PNG)", "*. jpg; *. gif; *. PNG ");
Myfilereference. Browse ([imagesfilter]);
2. upload a file Step 2-upload a file
When the file is selected, the event. Select event is triggered. Therefore, you need to register the select event listener on the myfilereference object:
Myfilereference. addeventlistener (event. Select, onselect );
Onselect is a listener function in which you can obtain the name of the selected file. For Security Sandbox reasons, only the selected file name can be obtained, and the complete file path cannot be obtained. After selecting the file to be uploaded, you need to call the upload () function to upload the selected file:
Myfilereference. Upload (uploadurl, myfilereference. Name );
The uploadurl parameter of upload is a URLRequest object, which specifies the URL used for uploading files. myfilereference. Name is the name of the selected file.
3. upload a file Step 3-upload progress
Generally, a progress bar indicating the upload progress needs to be displayed during the File Upload process, which provides a good user experience. You can use either of the following methods to display the upload progress. The first is to listen to the process event of the filereference object. You can get the upload progress in the listening function of the process event, and then set the progress bar. The second method is to bind the filereference object to the progress bar object. The progress bar automatically listens to the process event of the filereference object to display the upload progress.
4. Step 4 of File Upload-Upload complete
Sometimes, you need to give the user a prompt after the file upload is complete to tell the user that the file has been uploaded. Therefore, you must listen to the complete event of the filereference object. When the file upload is complete, the complete event is triggered. The user prompt or the processing after the file is uploaded must be implemented in the listener function of this event.
File Upload server processing:
The upload function of the filereference object is uploaded to the server in the following format:
Post/handler. cfm HTTP/1.1
Accept: text /*
Content-Type: multipart/form-data;
Boundary = ---------- ij5ae0ae0km7gi3km7ei4ch2ei4gl6
User-Agent: Shockwave Flash
HOST: www.example.com
Content-Length: 421
Connection: keep-alive
Cache-control: No-Cache

------------ Ij5gi3gi3ei4gi3ei4km7gi3km7km7
Content-Disposition: Form-data; name = "FILENAME"

Myfile.jpg
------------ Ij5gi3gi3ei4gi3ei4km7gi3km7km7
Content-Disposition: Form-data; name = "photo"; filename = "myfile.jpg"
Content-Type: Application/octet-stream

Content of the uploaded file
------------ Ij5gi3gi3ei4gi3ei4km7gi3km7km7
Content-Disposition: Form-data; name = "Upload"

Submit Query
------------ Ij5gi3gi3ei4gi3ei4km7gi3km7km7-
The server only needs to parse this structure, parse the "uploaded file content" part, and save it as a file. The file name is myfile.jpg.
Server-side Java code:

Private Boolean upload (httpservletrequest request) throws exception {</P> <p> int Len =-1; <br/> byte [] bt = new byte [4096]; <br/> string sperator = NULL; <br/> string filename = NULL; <br/> string savepath = "/"; <br/> // control the size of the uploaded file <br/> long filesize =-1; </P> <p> request. setcharacterencoding ("UTF-8"); <br/> servletinputstream in = request. getinputstream (); </P> <p> Len = in. readline (BT, 0, BT. length); <br/> If (Len! =-1) {<br/> sperator = new string (BT, 0, Len); <br/> system. out. println (sperator); <br/> sperator = sperator. substring (0, 28); <br/> system. out. println (sperator); <br/> Len =-1; <br/>}</P> <p> do {<br/> Len = in. readline (BT, 0, BT. length); <br/> string S = new string (BT, 0, Len); <br/> system. out. println (s); <br/> int Index = S. indexof ("filename =/" "); <br/> If (index! =-1) {<br/> S = S. substring (index + 10); <br/> Index = S. indexof ("/" "); <br/> S = S. substring (0, index); <br/> filename = s; <br/> Len =-1; <br/>}< br/>} while (Len! =-1); </P> <p> fileoutputstream out = new fileoutputstream (savepath + filename); </P> <p> Len = in. readline (BT, 0, BT. length); <br/> string S = new string (BT, 0, Len); <br/> system. out. println (s); <br/> int I = S. indexof ("Content-Type:"); <br/> if (I =-1) {<br/> system. out. print ("is not file"); <br/> return false; <br/>} else {<br/> in. readline (BT, 0, BT. length); // delete one line space <br/> Len =-1; <br />}</P> <p> long trancsize = 0; <br/> try {<br/> Len = in. readline (BT, 0, BT. length); <br/> while (Len! =-1) {<br/> S = new string (BT, 0, Len); <br/> If (S. length ()> 28) {<br/> S = S. substring (0, 28); <br/> If (S. equals (sperator) {<br/> system. out. println (s); <br/> break; <br/>}</P> <p> If (filesize! =-1) {<br/> If (trancsize> = filesize) {<br/> throw new exception ("file is too big "); <br/>}< br/> out. write (BT, 0, Len); <br/> trancsize + = Len; <br/> Len = in. readline (BT, 0, BT. length); <br/>}</P> <p> out. close (); <br/> return true; <br/>}catch (exception e) {<br/> out. close (); <br/> file TMP = new file (savepath, filename); <br/> If (! TMP. delete () {<br/> system. out. println (E. getmessage () + ", clear error"); <br/>}< br/> return false; <br/>}< br/>

Code supplement:
Parse the request content, parse the file content, and save it as a file. The file name can be parsed from the request content. To control the size of the uploaded file, set the value of The filesize variable.
Ii. File Download
File Download is relatively simple. Only three steps are required.
1. Create a URLRequest object
VaR DownLoadURL: URLRequest = new URLRequest ();
2. Specify the URL of the downloaded file
DownLoadURL. url = "http: // localhost: 8080/flexremotobject/download ";
3. Execute the download function of the filereference object. The first parameter of the download function is the URLRequest object created in 1, and the second parameter is the default file name of the downloaded file.
Myfilereference. Download (DownLoadURL, "image.png ");
After the preceding three steps are completed, the filereference object opens a File Save dialog box. After specifying a path, click Save to download the file.

Java code for file download:
Private void download (httpservletresponse response) throws exception {</P> <p> bufferedoutputstream Bos = NULL; <br/> bufferedinputstream Bis = NULL; <br/> try {<br/> Bis = new bufferedinputstream (New fileinputstream ("/head. PNG "); <br/> Bos = new bufferedoutputstream (response. getoutputstream (); </P> <p> byte [] buff = new byte [1, 2048]; <br/> int bytesread; </P> <p> while (-1! = (Bytesread = bis. read (buff, 0, Buff. length) {<br/> Bos. write (buff, 0, bytesread); <br/>}< br/>} catch (final ioexception e) {<br/> E. printstacktrace (); <br/>}catch (exception e) {<br/> E. printstacktrace (); <br/>}finally {<br/> If (Bis! = NULL) <br/> bis. Close (); <br/> If (Bos! = NULL) <br/>{< br/> Bos. flush (); <br/> Bos. close (); <br/> Bos = NULL; <br/>}< br/> response. flushbuffer (); <br/>}

Conclusion: To upload multiple files at the same time, you can use the filereferencelist class.

Full: http://download.csdn.net/source/1815266

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.