Flex2 FileReference API for Uploading Files
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 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:
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.