View comments | RSS feed

upload (FileReference.upload method)

public upload(url:String, uploadDataFieldName:String, testUpload:Boolean) : Boolean

Starts the upload of a file selected by a user to a remote server. Although Flash Player has no restriction on the size of files you can upload or download, the player officially supports uploads or downloads of up to 100 MB. You must call FileReference.browse() or FileReferenceList.browse() before calling this method.

Listeners receive events to indicate the progress, success, or failure of the upload. Although you can use the FileReferenceList object to let users select multiple files to upload, you must upload the files one by one. To do so, iterate through the FileReferenceList.fileList array of FileReference objects.

The FileReference.upload() and FileReference.download() functions are nonblocking. These functions return after they are called, before the file transmission is complete. In addition, if the FileReference object goes out of scope, any upload or download that has not yet been completed on that object is cancelled upon leaving the scope. So, be sure that your FileReference object will remain in scope for as long as the upload or download could be expected to continue.

The file is uploaded to the URL passed in the url parameter. The URL must be a server script configured to accept uploads. Flash Player uploads files using the HTTP POST method. The server script that handles the upload should expect a POST request with the following elements:

Here is a sample POST request:

 Content-Type: multipart/form-data; boundary=AaB03x
 --AaB03x 
 Content-Disposition: form-data; name="Filedata"; filename="example.jpg" 
 Content-Type: application/octet-stream
 ... contents of example.jpg ... 
 --AaB03x-- 

To send POST parameters to the server, set the value of FileReference.postData to your parameters. You can send GET parameters to the server with the upload() call by appending parameters to the URL.

If the file to be uploaded is bigger than approximately 10 KB, Windows Flash Player versions first send a test upload POST with zero content prior to uploading the actual file in order to verify that the transmission is likely to be successful. The second POST contains an actual file content. For smaller files, Flash Player does a single upload POST with the file to be uploaded. The Macintosh players currently do not do test upload POSTs.

Note: If your server requires user authentication, only SWF files running in a browser--that is, using the browser plug-in or ActiveX control--can provide a dialog box to prompt the user for a user name and password for authentication, and only for downloads. For uploads that use the plug-in or ActiveX control, and for uploads and downloads that use the stand-alone or external player, the file transfer fails.

When using this method, consider the Flash Player security model:

For more information, see the following:

Availability: ActionScript 1.0; Flash Player 8

Parameters

url:String - The URL of the server script configured to handle upload through HTTP POST calls. The URL can be HTTP or, for secure uploads, HTTPS.

uploadDataFieldName:String - The field name that precedes the file data in the upload POST. This parameter is supported in Flash Player 9.0.28.0 and later, only. The uploadDataFieldName value must be non-null and a non-empty String. By default, the value of uploadDataFieldName is "Filedata":

 Content-Type: multipart/form-data; boundary=AaB03x
 --AaB03x 
 Content-Disposition: form-data; name="Filedata"; filename="example.jpg" 
 Content-Type: application/octet-stream
 ... contents of example.jpg ... 
 --AaB03x-- 

testUpload:Boolean - A setting to request a test file upload. If testUpload is true, then for files larger than 10 KB, Flash Player will attempt a test file upload POST with a Content-Length of 0. The purpose of the test upload is to check whether the actual file upload will be successful and whether server authentication, if required, will succeed. By default, testUpload is false. At this time, a test upload is done only for the Windows players.

You can send GET parameters to the server with the upload() call by appending parameters to the URL; for example, http://www.myserver.com/upload.cgi?userID=jdoe

On some browsers, URL strings are limited in length. Lengths greater than 256 characters may fail on some browsers or servers.

Returns

Boolean - A value of false in any of the following situations:

Example

The following example shows an implementation of the upload() method by first prompting the user to select a file to upload, then handling the onSelect and onCancel listeners, and finally handling the results of the actual file upload.

import flash.net.FileReference;

var allTypes:Array = new Array();
var imageTypes:Object = new Object();
imageTypes.description = "Images (*.jpg, *.jpeg, *.gif, *.png)";
imageTypes.extension = "*.jpg; *.jpeg; *.gif; *.png";
allTypes.push(imageTypes);

var listener:Object = new Object();

listener.onSelect = function(file:FileReference):Void {
    trace("onSelect: " + file.name);
    if(!file.upload("http://www.yourdomain.com/yourUploadHandlerScript.cfm")) {
        trace("Upload dialog failed to open.");
    }
}

listener.onCancel = function(file:FileReference):Void {
    trace("onCancel");
}

listener.onOpen = function(file:FileReference):Void {
    trace("onOpen: " + file.name);
}

listener.onProgress = function(file:FileReference, bytesLoaded:Number, bytesTotal:Number):Void {
    trace("onProgress with bytesLoaded: " + bytesLoaded + " bytesTotal: " + bytesTotal);
}

listener.onComplete = function(file:FileReference):Void {
    trace("onComplete: " + file.name);
}

listener.onHTTPError = function(file:FileReference):Void {
    trace("onHTTPError: " + file.name);
}

listener.onIOError = function(file:FileReference):Void {
    trace("onIOError: " + file.name);
}

listener.onSecurityError = function(file:FileReference, errorString:String):Void {
    trace("onSecurityError: " + file.name + " errorString: " + errorString);
}

var fileRef:FileReference = new FileReference();
fileRef.addListener(listener);
fileRef.browse(allTypes);

See also

browse (FileReference.browse method), browse (FileReferenceList.browse method), download (FileReference.download method), fileList (FileReferenceList.fileList property)


Flash CS3


Comments


Elimegrover said on Jun 26, 2007 at 7:40 AM :
Is there anything that is being done to correct the Precondition Failed bug when attempting to upload files to a server that uses mod_security? This is rather frustrating as currently, the only method available is to disable mod_security for the particular directory/application.
NightFox80 said on Jun 27, 2007 at 2:44 AM :
I have IOError #2038 problem!!! I tried on localhost server and everything works good, but when I set parameters in FLEX for the real webserver (Aruba.it), comiple and deploy onto the webserver I encounter this error. The folder where I work has got 777 permissions. I tried also with a crossdomain.xml policy or with .htaccess file to disable security controls (as someone said in other blogs), but nothing solved the problem...
The path of my upload.php is correct and the script too is sintax correct and it works on localhost.
My Flex application starts uploading files and after 1-2 seconds they are completed I receive the IOError #2038. Someone said also that could be in the MIME type of the upload (but why does it work on localhost?). Does someone suggest me a solution? Thanks!
macrr said on Jul 13, 2007 at 7:47 AM :
Can I catch an event that notifies of failure if user attempts to upload a file that *local file permissions* allow browsing to the file is not readable by current user. For example, a file is chmod 220 or current user doesn't have ability to read file on a Windows box.
Kory75 said on Oct 26, 2007 at 1:24 PM :
After using FileReference
.upload("myscript.php")

the php handling the uploaded files perfectly.
however I'm not able to reach any session variable in php
what is very annoying.

I have an another php call in the same swf
what call an another php using URLLoader .load(function)
there this problem is not happening.
if anybody has any solution, please let me know.
ccharlton said on Dec 27, 2007 at 11:34 AM :
If you have session issues, here's a blog post that will help:

http://thanksmister.com/?p=59
No screen name said on Sep 18, 2008 at 8:04 AM :
Is there any way to read the filename that was saved to the server? My php script renames the file if there is a duplicate filename. I can't find any info on this anywhere?
ntoo said on Jan 16, 2009 at 11:46 AM :
The sample (and example) POST request has
filename="example.jpg"

That should be
Filename="example.jpg"

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flash/9.0/main/00001679.html