FileReference:Class

The Flash FileReference Class is a tool for uploading files by:

  1. Popping up a browse box, prompting the user to define the file they would like to upload
  2. Uploading the required file, using either php, coldfusion, or asp.
  3. Keeping Flash up to date on the progress of uploading the file.
  4. And returning to Flash either an error or else it notifies Flash that it uploading has been completed successfully.

The properties of the FileReference :Class are as follows:

  1. addListener(listener:Object) :Function
  2. browse(typelist:Array) :Function
  3. cancel:Function
  4. creationDate:Date[read-only]
  5. creator:String[read-only]
  6. download
  7. modificationDate:Date[read-only]
  8. name:String[read-only]
  9. onCancel:Function -runs when user cancels
  10. onComplete:Function -runs when uploading completes
  11. onHTTPError:Function -runs when a HTTPError is encountered
  12. onIOError:Function -runs when an IOError is encountered
  13. onOpen:Function -runs when file is Opened
  14. onProgress:Function -runs when upload progress is made
  15. onSecurityError:Function -runs when a security error is encountered
  16. onSelect:Function -runs when user selects which file to upload
  17. removeListener:Function
  18. size:Number[read-only]
  19. type:String[read-only]
  20. upload:Function

Judging by the number of people having problems with the FileReference class, a list of causes of errors would be helpful. If an error you are currently having is not listed, then please feel free to comment on it.

  1. 406 error
    • This might be able to be fixed by going to the root directory on your site, and adding the following lines to .htaccess (if it is not there, then create it)
        SecFilterEngine Off
        SecFilterScanPOST Off
    • or, on the other hand, if your page is under a secure connection,
      Flash cannot upload under a secure connection, so if you are trying for example to upload to https://mysite.com/upload.php, it won’t work, because flash cannot upload under a secure connection, however, you can upload to http://mysite.com/upload.php
    • or a combination of both
  2. If you would like to have an error listed, please comment on your error, and an administrator may add it to the blog

Also included in this blog is a list of other articles on the FileReference Class and it’s errors

The code for php is:

$fileName = basename($_FILES[”Filedata”][”name”]);
$uploadFile = “images/uploaded/” . $_GET[”date”] . “-” . $fileName;

if (move_uploaded_file($_FILES[”Filedata”][”tmp_name”], $uploadFile)) {
chmod(dirname(__FILE__) . “/” . $uploadFile, 0777);
print 1;
}else{
print 0;
}
?>

The code for coldfusion:

is available here

The code for asp:

<%@LANGUAGE=”VBSCRIPT” CODEPAGE=”1252″%>
<%

Set theForm = Server.CreateObject(”ABCUpload4.XForm”)
theForm.Overwrite = True ‘ overwrite existing files
.
For Each theFile in theForm.Files
If theFile.FileExists Then
‘ save in virtual directory
theFile.Save “temp\” & theFile.FileName
Response.Write(”1 ” & theFile.FileName)
Else
Response.Write(”0″)
End If
Next

Set theForm = nothing

%>

If you have any comments or questions please feel free to let us know… and
Thanks for reading
…Also, a tutorial on this is coming soon!

29 Responses to “FileReference:Class”

  1. awflasher Says:

    I am always experiencing the I/O Errors.
    It seems when the connection status is bad (as a result from pinging to server) , the I/O error triggers.
    Do u have any good idea for handle with this kind of I/O Errors. As any idea for supervention-uploading after I/O error….

  2. admin Says:

    So it works some of the time,
    yet other times “when the connection status is bad”,
    it does not work at all?

    Would you mind posting the code for uploading?

  3. admin Says:

    Also, how far does it get in the upload process, in bytes or on percent?

  4. wraevn Says:

    I’m getting HTTP 500 errors.

    I’m using the FileReference class to upload images to an aspx page. Basically it goes like this:

    Flash uploads the file to http://www.mydomain/upload.aspx

    The aspx page calls a webservice that sends the image to another server (my image server). I’m of course not sending it directly to the image server because Flash can’t do that (it’s on another server)

    It looks like Flash is successfully uploading the image (100%) and then I get the HTTP 500 error. It seems to work when I’m testing locally, but once the .swf goes live on the server, no go. The .swf and the .aspx page are in the same directory.

    I’d also like to know more about the 500 error. w3.org says the following, but it’s still so vague:

    10.5.1 500 Internal Server Error
    The server encountered an unexpected condition which prevented it from fulfilling the request.

    Please assist!!

  5. admin Says:

    Just wondering what code you are using for your upload
    Also, are you making sure to allow localhost and whatever other sites you have to access
    for instance:
    System.security.allowDomain(”http://localhost”, “127.0.0.1″);

  6. admin Says:

    awflasher:
    your problem is caused by uploading a large file, and the resetting of connection happens too often to allow the whole file to upload
    You can fix this by finding php.ini on your server, and editing it or if you do not have any php.ini on your site, go to this link and find out how to create one.
    http://www.washington.edu/computing/web/publishing/php-ini.html#register_globals

  7. admin Says:

    wraevn:
    Just thinking that maybe the reason that flash is having the errors is something to do with calling another service(”The aspx page calls a webservice that sends the image to another server (my image server).”)
    Have you made sure that flash didn’t actually upload the image, even though it got the error, it may still have uploaded it?

  8. wraevn Says:

    Well, it’s all figure out … it turned out that the upload WAS working, in that the image was making it to the image server, but the image server (running DotImage by Atalasoft) was at v. 5.0, and the website .dlls were published using v. 3.0. In addition, it was trying to convert an image format to the same image format and was throwing an exception for that.

    My AS was perfect :-P

    It just shows to go ya!

    However, your comments are all valid, and are definitely things I would’ve looked into should the problem have been on my side. The Security is a big one and all that stuff is being set properly, so good call there.

  9. Ben Says:

    Hi,

    I’ve got a problem with an upload, using php and the as.
    My file is downloading to 100% but on firefox mac I’ve got an 404 error and on firefox pc got a 406 error…
    I’ve tried the secfilterengine and other in .htaccess but it doesn’t work and crash the server…
    Somebody can help me???

    Ben.

  10. Protoflash Says:

    Hi Ben,
    Just wondering if your page is under a secure connection. ie. is it at https: or http:
    Also, if you would like to post the code you are using I can look through that.
    Cheers,
    Protoflash

  11. Ben Says:

    Hi thank you very much…
    My website is in http://

    and the code for the php is:

    and for the as:
    var domain:String = “www.mydomain.com”;
    var url:String = domain + “/test/admin/”;

    //Allow this domain
    System.security.allowDomain(domain);
    import flash.net.FileReference;

    upload_button.button_text.text = _root.title;

    // The listener object listens for FileReference events.
    var listener:Object = new Object();

    loading._x = Stage.width - loading._width;

    var screen:Object = new Object();
    screen.onResize = function(){
    loading._x = Stage.width - loading._width;
    }
    Stage.addListener(screen);

    loading._visible = false;

    listener.onSelect = function(selectedFile:FileReference):Void {
    selectedFile.upload(url + “includes/uploadFile.php?upload_dir=” + _root.upload_dir);
    loading._visible = true;
    file_name.text = “”;
    }

    upload_button.loading._xscale = 0;

    listener.onProgress = function(file:FileReference, bytesLoaded:Number, bytesTotal:Number):Void {
    upload_button.loading._xscale = bytesLoaded / bytesTotal * 100;
    file_name.text = Math.round(upload_button.loading._xscale) + “% - “+bytesLoaded+” bytes of “+bytesTotal+” total”;
    }

    listener.onOpen = function(selectedFile:FileReference):Void {

    }

    /* ERRORS */
    listener.onHTTPError = function(file:FileReference, httpError:Number):Void { file_name.text = “http error: ” + httpError + ” dir: ” + _root.upload_dir; }
    listener.onIOError = function(file:FileReference):Void { file_name.text = “IO error”; }
    listener.onSecurityError = function(file:FileReference, errorString:String):Void { file_name.text = ” security error: ” + errorString; }

    function onLoadInit(){

    }

    loader = new MovieClipLoader();
    loader.addListener(this);
    loader2 = new MovieClipLoader();

    listener.onComplete = function(selectedFile:FileReference):Void {
    loading._visible = false;
    upload_button.loading._xscale = 0;
    if(_root.hidden){
    getURL(”javascript:setHidden(’”+_root.hidden+”‘, ‘”+ selectedFile.name+”‘, ‘” + _root.upload_dir + “‘);”);
    }else{
    getURL(”javascript:setImage(’”+_root.image_tag+”‘, ‘”+ selectedFile.name+”‘, ‘” + _root.upload_dir + “‘);”);
    }
    file_name.text = selectedFile.name;

    _root.remove_visible = “true”;
    }

    if( _root.image_name == “” ){
    _root.remove_visible = “false”;
    file_name.text = “”;
    } else {
    _root.remove_visible = “true”;
    file_name.text = _root.image_name;
    }

    remove.onEnterFrame = function(){
    if( _root.remove_visible == “false” ){
    this._visible = false;
    //file_name.text = “”;
    } else {
    this._visible = true;
    //file_name.text = _root.image_name;
    }
    }

    remove.onRelease = function(){
    file_name.text = “”;
    if(_root.hidden){
    getURL(”javascript:setHidden(’”+_root.hidden+”‘, ”, ”);”);
    }else{
    getURL(”javascript:setImage(’”+_root.image_tag+”‘, ”);”);
    }
    _root.remove_visible = “false”;
    }

    var imageFile:FileReference = new FileReference();
    imageFile.addListener(listener);

    upload_button.onPress = function(event:Object):Void {
    //imageFile.browse([{description: “Images”, extension: “*.jpg;*.gif;*.png;*.jpeg”}, {description: “Documents”, extension: “*.ppt;*.zip;*.rar;*.ai;*.psd;*.doc;*.xls;*.flv;*.eps;*.pdf”}]);
    imageFile.browse([{description: “Documents”, extension: “*.jpg;*.gif;*.png;*.jpeg;*.pdf;*.doc;*.xls;*.ppt;*.pps”}]);
    }

    Thanks in advance…

    Ben.

  12. Protoflash Says:

    Just with a quick look at your code. Try using
    System.security.allowDomain(”http://localhost”, “127.0.0.1″);
    instead of System.security.allowDomain(domain);
    You may get better results this way.

  13. Ben Says:

    Hi,

    but this is on a website…
    It’s not in local…
    Where do I put my I.P. for the server?

    Thanks in advance,

    Ben.

  14. Protoflash Says:

    Hi,
    Allowing Localhost will allow the actual site.

  15. Ben Says:

    Hi,

    okok, I can see it’s close to work.
    The loading goes to 100% and write the name of the image… but the copy doesn’t work…
    Do you have an idea about it???

    Thanks again!!!

    Ben.

  16. Ben Says:

    Hi,

    I’ve find my error:
    POST instead of GET in the php…
    Thank you very much for everything…

    Ben.

  17. Protoflash Says:

    No worries, mate. I’m just sorry I didn’t think of that.
    btw. I am a flash freelancer, so if you have any work you would like to pass on to us please feel free to contact me at protoflash.com | at | gmail.com
    Cheers,
    Protoflash

  18. demiurgu Says:

    I am trying to upload a file using rails server side script. …. it is working in safari but in IE and FF i get 406 error in server log…

    Some ideas ?

  19. Protoflash Says:

    Try creating a .htaccess file in your root directory with:

    SecFilterEngine Off
    SecFilterScanPOST Off

    If that doesn’t work, move the .htaccess file to the directory of the .swf file.

    This might do the trick…

    If it doesn’t, do you know what percentage the upload is getting to before it comes across the error?

    Can you possibly give me a link so I can see exactly what is happening?

  20. PJ Says:

    Re: “Flash cannot upload under a secure connection”…

    I have a secure server and I can upload just fine using Flash / PHP over https… but it won’t with FireFox and apparently it is related to a .htaccess setting (which is what led me to this thread). IE6 & IE7 can and do upload over a secure connection… so maybe I’m doing something different? Who knows.

  21. Protoflash Says:

    Try uploading over http:, that might do the trick….

  22. Jim Says:

    I have my flash 8 (AS 2.0) upload working via fileReference.upload using a coldfusion upload.cfm page.

    The problem is that I can upload small files but larger ones such as a 1 meg file upload to 100 % but return with an error code 500 and fails to transfer over http.

  23. Protoflash Says:

    500: This error could be due to many things, but it is an “Internal server error”. Issues to check:
    - You are uploading files larger than the allowed post size set in the CF administrator. See the setting “Maximum size of post data” in the Settings page of the CF administrator. The default I think is 100MB
    - You are uploading large files and CF is running out of memory. CF has to put the whole size in memory while it gets uploaded before it saves it to disk. If you have access to the logs (the java output logs, not the regular cf logs), check for this error.

    http://www.asfusion.com/blog/entry/file-upload-with-coldfusion-flash-forms

    Let me know how it goes…

  24. Nate Says:

    500 error here as well. I’ve tried everything- googled myself into the ground. htaccess file in root and swf folder breaks the server. Tried the domain security (AS) patches, and even the cross domain file, still no luck. Just gets to 100%, and throws the 500 error. The same exact script (I used for ever) works on all my other servers, but of course, not the one my clients are using. Tried every folder permission combo also - 703, 704, 777, etc etc etc. Godaddy won’t help - to them, this is a “scripting” issue, even though the same exact script works on a neighboring server.

  25. William Says:

    Hi,
    I’m looking for ages for an explanation of this sentence in Flash doc :
    “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”

    Why ‘officialy support 100MB’ ? what could happen if I upload 101MB or 200MB ?

    thanks for any hint or links

  26. Protoflash Says:

    Hi William,
    I’v got a hunch that it’s because the .htaccess files on the server, etc. stop it from uploading more than 100MB normally, because the page times out. So I think you’d have to modify them or look for some other server-side settings.
    Other than that, I can’t think of anything to suggest except maybe try it!
    Cheers,
    Stephen

  27. Imtiyaz Says:

    Hi,

    I am using FileReference.upload(”upload.php”). Uploading works well in localhost but when i put in on server no copy function seems to work. I even tried you option of System.security.allowDomain(”http://localhost”, “127.0.0.1″);

    But fails. Can you guide me where i am wrong in implementing on server. U can have a look at:http://imtiyaz.byethost3.com/testing/upload.html

    Waiting fr reply.

    Best Regards,
    Imtiyaz

  28. Soliton Says:

    Hi
    i run a filereference.upload test flex application which uploads a file through a coldfusion server.
    i’m using flex 3 and coldfusion 9.
    i’m also passing parameters in the URLRequest that points to the upload cfm file.
    when i run my test using the built-in web server (Jrun) all seems to work just fine
    when i run the same test on a coldfusion instance that runs on Apache web server, it throws a I/O Error…

    i suspect i’m missing some kind of a configuration i need to perform when moving to a production setup rather then a development environment…

    can anyone guide me through this?
    does it have to do with security? on the client? on the server?
    i’m still running on localhost…

    cheers
    :)

  29. Protoflash Says:

    Hi Soliton,

    I’m guessing it is the filesize. Check the MaximumFileSize setting of your webserver.
    Let me know how it goes. :)

    Cheers,
    Stephen

Leave a Reply