Flex2 FileReference API for Uploading Files

Posted on June 27, 2006 | 7 comments

I read through the article titled, “Using the FileReference API to upload files in Flex 2 applications”. It is a good starting point for learning how to use the file upload capabilites in Flex 2 and Flash Player 9.

I found some simple instructions missing that confused me for a little bit.

The article mentions this paragraph

When uploading a file, developers often request to attach additional metadata such as date/time at upload, auther name, etc. With Flash 8 FileReference, you can’t attach that metadata in the POST data, other than through the URL parameter. The uploadDataFieldName parameter now allows for uploading metadata. Below is an example of the uploadDataFieldName field (name=”author=John”) in the network trace result.

Its talking about setting the form-data name field for meta-data purposes. “uploadDataFieldName” is the 2nd parameter that is passed into FileReference.upload(). The article talks about using it for metadata purposes. What I found was that the “author=John” is the actually field name for the temporary file in ColdFusion and that there is a better way to pass data along with the post.

Here is a snippet of my code:

var param:String = "TempFilename" ;
var req:URLRequest = new URLRequest( endpoint );
var urlVars:URLVariables = new URLVariables();
urlVars.author = txtAuthor.text;
urlVars.description = txtDescription.text;
urlVars.remotefolder = txtRemotefolder.text;
req.data = urlVars;                    
req.method = URLRequestMethod.POST;
fileRef.upload(req, param, false);

Then on the ColdFusion side you can simple access the parameters like this:

<cfscript>
    author = form.AUTHOR;
    desc = form.DESCRIPTION;
    remoteFolder = form.REMOTEFOLDER;
    filename = form.FILENAME;
    tempFile = form.TEMPFILENAME;
</cfscript>

<cffile action="move"
   source="#tempFile#"
   destination="#remoteFolder#/#filename#" />

This all seemed a simple example that could have been included in the TechNote or the examples in the FileReference or FileReferenceList classes. It is quite nice to have such a nice way to upload and download files now.

  • Todd Clare

    I was looking for this info all day yesterday! Thanks for posting this.

    Any thoughts on how to post the file, along with the “metadata” fields, and also get a reply back from the server?

    I’m posting to a PHP page, and I’d like to receive a response (like the XML I send from my other HTTPService-s) that contains relevant information (file created successfully, new entry was made to the database, here’s its ID)? I’m new to the FileReference, but can;t seem to figure it out.

  • http://lailablog.blogspot.com Laila

    Thank you so much, works like a charm! I’m a bit puzzled that this solution isn’t mentioned in the article you refer to though, it really solves the metadata issue!

  • me

    Hi,geat stuff. Howwould you do more of an FTP application with this code. Like create and delete directories and files.

    Thanks.

  • Ramon

    Hi, i have a problem with that code. I’ve tryed to put some characters and the encoding doesn’t work. For example, if i try to send a description including “ñ” or “ó”, it doesn’t work :_(

    If anybody can help me, i’ll be grateful for your support.

    Thanks.

  • C

    How to I handle special cases when for example the server side detemines that the file is too big or invalid format or has already been uploaded. I would like to know how I can read data from the server’s HTTP Response ?

    Or maybe there is a different mechanism that will allow me to let my user know that status ? I need to let them know what’s the reason for upload failure…

  • robson

    hello, i am trying to get this to work with jsp, but request.getParater(“benutzerID”) is alway null:

    here is my as code:
    var sendVars:URLVariables = new URLVariables();
    sendVars.benutzerID = “1″;

    var request:URLRequest = new URLRequest();
    request.data = sendVars;
    request.url = _strUploadScript;
    request.method = URLRequestMethod.POST;

    _refUploadFile.upload(request, “file”, false);

    can anyone help?

  • http://www.canaljava.com.br mindu

    HI
    I have the same problem of Ramon


    Hi, i have a problem with that code. I’ve tryed to put some characters and the encoding doesn’t work. For example, if i try to send a description including “ñ” or “ó”, it doesn’t work :_(

    some solution ?
    thanks