A Round Trip with FileReference

Last night I presented the session I wrote for webDU at the London user group. Due to there being a lot to cover and lack of time I nowhere near got through the whole presentation only really getting to cover the first 3 examples on FileReference, and taking a quick look at the main class in the AS 2.0 ‘Round Trip’. I didn’t actually really get to cover any of the AS in the ‘Round Trip’, or any of the server side stuff either.

You can view the webDU presentation here, and the London user group presentation here. They are both the same except they have slightly different graphics.

If there’s enough people who actually want me to go through the ‘Round Trip’, then post your requests in the comments here or on the London user group forum and I’ll see what I can do.

All the source for the examples in the presentation can be downloaded from the ‘Download Example’. If anyone has any questions whether it be the AS, the PHP, or just working with classes, don’t be shy! Post your questions here and I will do my best to answer them.

I’m going to concentrate on developing the Flex 2.0 version (its beta 1 at the moment) for a future talk at any conference that will have me.

49 Responses to “A Round Trip with FileReference”

  1. Aran says:

    Hi Tink.

    We are currently rolling in a flash based file upload into an app, and I had a question.

    - have you gotten fileReference to work on all major browsers through SSL (with ANY serverside language)?

    we have gotten it to work in IE, but Firefox just seems to ignore the upload button click (don’t get a error message being returned to flash, and service capture shows no traffic from flash at all). We are using a servlet through jRun4.

  2. Tink says:

    Hi Aran

    Unfortunately I have no experience using FileReference through SSL.

    There are some comments about it on LiveDocs. Although as I understand from your comment, Flash isn’t even trying to send the data, which doesn’t seem to be the same problem as others are experiencing.

    ———————————————————————-

    nectil said on Dec 12, 2005 at 4:05 AM :
    !! Warning for Mac player !!
    When you use the FileReference.upload(), the class add the port 80 to the request! For example, with the url :

    http://www.nectil.com/upload.php

    The real query will be sent at

    http://www.nectil.com:80/upload.php

    Please, Flash player team, remove the :80 !!! It’s impossible to send it over HTTPS for mac users! thanks.

    ———————————————————————-

    xbrotherx said on Dec 20, 2005 at 10:37 AM :
    I have had a similar situation using HTTPS on both Mac and Windows Flash Players. I can get .upload to work correctly on Mac Safari and Windows IE through HTTPS if I put the fully-qualified path, including the SSL port 443, in the url parameter like so:

    file.upload(“https://www.somesite.com:443/cgi-bin/someScript.sh”);

    Just using the following does not work through HTTPS:

    file.upload(“/cgi-bin/someScript.sh”);

    And neither of them work on Mac Firefox or Windows Firefox.

    ———————————————————————-

    I’m sorry I can’t be of more help. I’d be really interested in your results though. Will you be posting them anywhere?

  3. Aran says:

    Tink.

    I am pretty much in the same situation as “xbrotherx”. SSL just doesn’t work under firefox (pc/mac). I’ve read all the livedoc comments before posting here (and trolled the web), and no one seems to know anything. I couldn’t even find a complete working example of a .jsp/servelt upload, so we had all sorts of fun getting the upload to work at all (apache commons also just doesn’t work with flash uploading due to flash sending corrupted post data, which is the most used uploading library for J2ee). Blah. I’m giving up for the moment. I’d be happy to post back to this blog if we do get it working.

  4. [...] t, saw an interesting dip into component creation from Mike Jones, and a second sitting of Tinks ‘Round trip with file reference’ talk . I captured Tinks talk on my t [...]

  5. Gregory says:

    Hi, everyone!
    Take a look at nice free Flash Upload software tool http://www.element-it.com/MultiPowUpload.aspx

    It also includes a lot of working examples of server-side upload scripts like PHP, ASP, PERL, ASP.NET.

  6. Damon says:

    Hi all, just for the record, I too am trying multi file upload from flash 8 in firefox over ssl, and I’m having the same problem as Aran – the FileReference.upload() call from flash just seems to be ignored.
    I added some extra error checking, and I can see that it calls onIOError() for each selected file that I try to upload, but all it tells me is the file name, and not any more info on why it failed! So frustrating. It really seems like they just didn’t test this in firefox at all.

  7. Teevio says:

    Hi Tink, I wish I knew a better way to contact you, but I have a question regarding filereference. Please reply via my email address.

    Basically, I have created an uploader that works perfectly on the Mac, on all browsers.

    The uploader works on Windows, but some of the time, regardless of the browser. I have tested IE and Firefox. The problem is that around 10-15 mbs of upload flash starts losing packets for no apparent reason and can’t recover. It eventually gives an IO error. Remember that this happens only some of the time, but occurs often enough that it makes the flash uploader impossible to use for files over 10 megs.

    I’m wondering if you have encounter this problem and if there is any way around it. I could not find any useful information anywhere regarding how to fix this problem.

  8. Tink says:

    Hi Teevio

    I’ve not encounter this problem or heard of anyone else encountering it.

    This is the best way to contact me. If i give out my email I would never get any work done.

    Maybe you can link to an example and source here and I will take a look.

  9. Michael says:

    Yeah there’s a problem with uploading through SSL via Firefox.. I/O error returned.. been searching through the web, tries different solutions etc2 for no avail

  10. Christian says:

    Hi there, I’ve been looking everywhere but can’t understand why in my jsp implementation the post for upload from flash sends the form but all query string data appended as params on the url string are lost (say: “/uploadFileFla.jsp?id=1″), the id=1 will be lost.
    Please help !!!

    thnks.
    cC

  11. Damien says:

    Hi everybody,

    I have exactly the same problem as Teevio. The problem is that around 10-15 mbs of upload flash I have an IO error. I don’t know why.

    If someone knows the solution…

    Thank you
    Damien

  12. Kisakookoo says:

    Hi! Why I can’t fill my info in profile? Can somebody help me?
    My login is Kisakookoo!

  13. Tink says:

    Sorry Kisakookoo, I don’t understand?

  14. Arie says:

    Hi all,
    I’ve got a different problem, but I hope someone can help me out. I’ve created 2 flash movies for uploading. The first one only contains a browse button for locating the file to upload. The second one is the progress movie, both movies communicate to each other. Everything is working fine, however it seems that I can upload anything without problems as to some others who do upload the files don’t get through. I’ve checked my PHP code and all other things, but I a have no clue why some people have trouble and others don’t.

    I’ve asked specs from the people who had problems, but they are all using flashplayer 8+ and my code is working since i’m able to upload. The only other thing I can think of is a domain related problem. It seems that Dutch people (including myself, cause my server is located in Holland) don’t have a problem while others (most abroad) do have a problem.

    Can this be an issue in flash? I’m wondering cause I’m out of possibilities.

    Hope someone had this same problem though.

  15. Tink says:

    Do you have a test version anywhere Arie?

  16. Arie says:

    Yea I have Tink, can I send you a personal message? Otherwise I’ll post the info later on today.. :)

  17. Arie says:

    Tink here’s the information: test.ratemycreation.com
    htaccess login: testrmc
    htaccess pw: test

    Once you are on the site you can login under ddream with the password test. Then under upload you can upload a mp3 or wav and see if the upload goes through.

    Thanks anybodies help.. :)

    Btw this is a testversion of the site, so no worries about messing up things.. :)

  18. Tink says:

    Nope don’t work for me. It’s like its doesn’t even attempt to upload.

    Can you try running my version on your server and make sure that works for you. Then you can isolate whether it is a server issue or whether its an issue with your code.

  19. Arie says:

    Hi Tink,
    I’ve put your file upload on the website and tested it with an upload. For me it works just fine as did my version. So hopefully you are able to upload something too..

    Thanks for helping me out to determine the problem.. :D

  20. Tink says:

    I get an HTTPError

    You need to change the code so it outputs the actual HTTP error to the TextArea and then hopefully we can find out whats wrong.

  21. Arie says:

    Hi again,
    I’ve changed the code to:
    private function onHTTPError(fileRef:FileReference, httpError:Number):Void

    And am printing httpError to the textArea. Is this what you mean? If so then you can try and upload again..

    Would be great if we can determine the problem.. :D

  22. Tink says:

    its 401 which means the request requires authentication, probably down to the .htaccess authentication u have in place.

    Flash does not provide a way to authentication with FileReference and therefore uploading fails.

  23. Arie says:

    Hi tink,
    I removed the .htaccess, since the regular site doesn’t need authentication, so I hope this time you will be able to upload something.
    :)

  24. Arie says:

    Hi,
    Strange why some people can’t upload then. Anyways I put back the upload I’m using, maybe it’s something in my programming causing uploads not to go through. If someone wants to try and upload something to see if it works please be my guest.. :) I appreciate any help…

    Thanks to Tink for testing and helping me out… :D

  25. Tink says:

    I can’t see anythng happen when i try and upload now so it must be the code your using. Can i suggest you re-write it and base it round my version that worked.

    Just start adding the dropdowns above the Flash, but keep everything else there so you can see what is outputted and the stage of the upload etc.

  26. Arie says:

    K, I reprogrammed the upload screen and built in the output for onIOError & onHTTPError, also included the onSecurityError.

    Again for me the upload works. So hopefully this will determine why you can’t upload.. :D

  27. Mike says:

    Hey, I have been trying to configure FileReference.upload() to post to a .jsp that requires authentication. I have read that you cannot authenticate on uploads, so I am trying to figure out a workaround.

    I have tested without authentication like this:

    fileRef.upload(“http://mydomain.com:8080/contextRoot/handleUpload.jsp”);

    When I turn authentication on, I actually get a prompt to login, which is cool – I guess authentication does work for uploads. The problem is that my usernames require an ‘@’ (username@organization) – this seems to not work

    I have tried replacing the ‘@’ with entities ‘%40′ and ‘&#64′, but they don’t work. Have you ever run into using the ‘@’ like this before with any type of authentication?

  28. Sastry says:

    Hi,

    We are facing issue with not being able to upload file using FileReference in FireFox using HTTPS protocol.The same works with HTTP protocol.

    We greatly appreciate any pointers in what must be done to get this working in FireFox.

    Thankyou in Advance.

  29. Mauricio says:

    Hi,
    I found your tutorial very useful and have used successfully in different situations. But now I’m facing a problem for which there seems to be very little information. I’d appreciate any insights on this issue:

    My application allows user to do either single or multiple file uploads. There are 2 buttons for each type. When either is pressed an instance of the FileReference or FileReference class is created.

    It works fine if only I do one type of successive uploads: single files only, or multiple files only. BUT whenever I switch from one type to another (i.e., I start uploading single files, and then press the button to upload multiple ones) I get the following error:

    Error #2041: Only one file browsing session may be performed at a time.

    How do I close or eliminate ‘file browsing sessions’? The complete event doesn’t seem to do much on this respect.

    Thanks so much.

  30. Tink says:

    Have your tried cancelling the current upload before starting another?

    Theres really not need to create a FilerReference object each time, why not just create one at the start and then reuse it.

  31. Mauricio says:

    Hi Tink,
    Thanks for your feedback.
    I figured part of my problems with what i was doing (like 3 weeks making the same error and scratching my head)

    One of my big mistakes is that in the process we changed from 2 buttons to a single one. Which calls the appropriate function (via a conditional and a listener) depending on some previous choices made by the user. i didn’t have a remove listener on the conditional so through testing and after an user had made any initial choices, no matter what further changes he/she made, the same listener was always used.

    Adding a remove listener on each branch of the conditional eliminated the Error 2041 report.

    Regarding the FileReference and FileReferenceList objects, I had to go against your advice and create a new one each time a single or multiple file upload is called.

    The reason for this is that to avoid keeping loading previous files (for instance if i uploaded 2 files via multiple upload, and then tried 1 via single upload, i ended up having 5 files!!) I had to declare the object in use null when the upload was completed. Then, in order to use it again a new one has to be created.

    Not sure if this is the best way to accomplish this, but it seems that finally things are working as expected.

    Thanks for looking into this.

  32. Brian says:

    I know this is an old post, but I have a question regarding the file reference. I’m working on using the download process. I’ve done this in the past for other clients. On a bands flash site, they want the ability for users to download mp3s on their media page, and once again I’ve done this in the past. Using the most basic of code, the bare bones, it won’t work. I don’t get any error messages returned either. Nothing. I try and print to a text field incase this is happening (any events being called upon selecting save location)…

    What I’m getting in the safari activity viewer is.. It actually calls out to the file, which does exist at that location listen, but I get in the status column a “Error WebKitErrorDomain 203″… Any suggestions. As I have said, I have done this before without problems. It seems to me maybe it’s a configuration on the server end? The permissions on the file should be set up fine.

  33. Zeev Halevi says:

    Just another voice: we are also having problems using https in firefox

  34. coloureFunk says:

    Hello, first off – nice one for developing this, it’s amazing! — However, I do have a question :) – Is it possible to send a variable to the upload.php script? I need to be able to change the file name that the user is uploading… with ID prefix in.

    Thanks so much in advance.

  35. coloureFunk says:

    p.s. I meant an additional variable :)

  36. Tony says:

    Hi Tink,

    I am going a bit nutso here. I am using the uploader and everything is golden in the land of ie, but in firefox it’s a different story. I am receiving no errors through the upload and am not using SSL. I tried adding the fully qualified path + port in the.upload() and still get no love.

    Has anyone figured out why this wont work in firefox 2?

    Best,

  37. Brett says:

    Hi Tink,
    I too am having issues with a fileReference uploader. I’m next to positive that the issue is in the php / server realm. For small files there is no problem across the board, once I had the php script return something I thought I had fixed it. But problems arise when I try to upload large files. The client this is for requires large uploads, so this is a huge problem. In every browser / platform combo the file uploads almost completely, and usually stalls out at different points every time. Sometimes one or two megs short (usually in safari), sometimes it transfers all of the data, but onComplete never fires, and the file doesn’t show up on the server. Here is the php I’m using:

    Any help would be a real life saver.
    Thanks so much,
    Brett

  38. Brett says:

    woops, left the php in its working form. Here it is again:

    move_uploaded_file($_FILES['Filedata']['tmp_name'], “/Uploads/”.$_FILES['Filedata']['name']);
    if ( move_uploaded_file( $_FILES['Filedata']['tmp_name'] , $uploadfile ) ) {
    header(‘HTTP/1.1 200 OK’);
    }else{
    header(‘HTTP/1.1 404 Not Found’);
    }
    chmod(“/Uploads/”.$_FILES[’Filedata’][’name’], 0777);
    echo ‘&bError=0&’;
    exit;

  39. Tink says:

    @ coloureFunk

    FileReference doesn’t officially support additional parameters in AS 2.0 but they were added to the AS 3.0 version of FileReference.

    @ Brett

    The Flash Player only officially supports uploads of up to 100MB. Anything over that and I guess you run the risk of the upload failing. I’ll see if I can find out som more detailed info, but no promises.

  40. Brett says:

    This even happens for files as small as 10 megs or so.

  41. Brett says:

    In case it matters, here is the AS too:

    import flash.net.FileReference;
    import flash.net.FileReferenceList;
    var myFileRefList = new FileReferenceList();
    var myList:Array = myFileRefList.fileList;
    var listener:Object = new Object();
    listener._parent = this;
    var fileNum:Number = 0;
    var fileNumP:Number = 0;
    var loading:Number = 0;
    var total:Number = 0;
    listener.onSelect = function(fileRefList:FileReferenceList) {
    var list:Array = fileRefList.fileList;
    var itemList:String = ”;
    var item:FileReference = new FileReference();
    for (var i:Number = 0; i

  42. Brett says:

    Sorry for cluttering the board, didn’t realize that it’s the less than / greater than symbols that turn text into code I just replaced them with the actual phrase:

    import flash.net.FileReference;
    import flash.net.FileReferenceList;
    var myFileRefList = new FileReferenceList();
    var myList:Array = myFileRefList.fileList;
    var listener:Object = new Object();
    listener._parent = this;
    var fileNum:Number = 0;
    var fileNumP:Number = 0;
    var loading:Number = 0;
    var total:Number = 0;
    listener.onSelect = function(fileRefList:FileReferenceList) {
    var list:Array = fileRefList.fileList;
    var itemList:String = ”;
    var item:FileReference = new FileReference();
    for (var i:Number = 0; i *is less than* list.length; i++) {
    item = list[i];
    if (i == 0) {
    itemList = item.name;
    } else {
    itemList = itemList+’\n’+item.name;
    }
    if (i == list.length-1) {
    myList = list;
    uploadList = itemList;
    }
    }
    };
    function loadMe(){
    i = fileNum;
    uItem = myList[i];
    uItem.addListener(listener);
    uItem.upload(“http://www.bespokestudio.com/upload.php”);
    }

    upload.onRelease = function() {
    loadMe();
    uploadProgress.progBar._xscale = 1;
    fadeIn = new mx.transitions.Tween(uploadProgress, “_alpha”, mx.transitions.easing.Strong.easeOut, uploadProgress._alpha, 100, 1, true);
    };
    listener.onCancel = function():Void {
    trace(“onCancel”);
    };
    listener.onOpen = function(file:FileReference):Void {
    trace(file.name);
    };
    listener.onProgress = function(file:FileReference, bytesLoaded:Number, bytesTotal:Number):Void {
    var percent = (bytesLoaded/bytesTotal)*100;
    this._parent.uploadProgress.progBar._xscale = percent;
    this._parent.uploadProgress.loadedText = file.name+’ ‘+Math.round(bytesLoaded/1024)+”"+”/”+”"+Math.round(bytesTotal/1024)+” k”;
    };
    listener.onComplete = function(file:FileReference):Void {
    if(fileNum *is less than* (myList.length -1)){
    fileNum += 1;
    loadMe();
    } else {
    fadeOut = new mx.transitions.Tween(this._parent.uploadProgress, “_alpha”, mx.transitions.easing.Strong.easeOut, this._parent.uploadProgress._alpha, 0, 1, true);
    uploadList = ”;
    fileNum = 0;
    }
    };
    listener.onHTTPError = function(file:FileReference, httpError:Number):Void {
    trace(“onHTTPError: “+file.name+” httpError: “+httpError);
    };
    listener.onIOError = function(file:FileReference):Void {
    trace(“onIOError: “+file.name);
    };
    listener.onSecurityError = function(file:FileReference, errorString:String):Void {
    trace(“onSecurityError: “+file.name+” errorString: “+errorString);
    };
    myFileRefList.addListener(listener);

    uploadBrowse.onRelease = function() {
    myFileRefList.browse();
    };

  43. David says:

    I am also having the problem of uploading large files. I tried changing the php.ini, editing the upload_max_filesize and post_max_size but still I get nothing.

    It’s strange because I am running it locally and it says it has uploaded fine, but nothing appears in the folder. I have noticed there are quite a lot of people on the net who are having the same problem. Any help greatly appreciated.

  44. Tink,

    I spoke WAY too soon. I can’t get this FileReference thing to work. Do you have a more layman version, perhaps? Something without all those components? I can’t figure out what a public function is, or a private function…I simply am baffled by the code. I guess I’m a real newbie. Please help.

    Truly,

    m

  45. My educated guess is that this is definitely a Flash/FileReference issue.

    I’m using a flash component to upload files and post them to my ASP.NET handler. This works fine with small files (i.e. 2MB), however, when I try to upload a 10MB file, it just hangs and it never actually posts the file to ASP.NET (I debugged to see if the file would come in, and just as I thought, it never even came to ASP.NET). I was able to reproduce this issue with FireFox 2 and IE 7.

  46. Mauricio says:

    not sure if this would help, but here are my 2 cents:
    -can you upload such file through a simple asp-html form? If not, then the problem can be found on the asp-server configuration limiting uploads to a maximum file size.

  47. I was able to upload files larger than 100MB when I had a regular html file input element on the page. I’m currently looking into several different Flash applications that use the FileReference to see if the issue persists with FileReference or if it’s just with the swf file that I was using… I’ll keep you posted.

Leave a Reply