A Flash Developer Resource Site

Page 1 of 2 12 LastLast
Results 1 to 20 of 25

Thread: Multi Uploads using FileReferenceList ();

  1. #1
    Senior Member
    Join Date
    Apr 2004
    Posts
    111

    Multi Uploads using FileReferenceList ();

    Whilst i have got the single file upload working i am having problems understanding the multiple file uploads.

    I can get the browsing for multiple fiels working fine using this:

    fileReflist = new flash.net.FileReferenceList ();
    fileReflist.addListener (fileUploadmulti);

    onBrowselst = function () {
    fileReflist.browse();
    };

    browsemulti.addEventListener ('click', onBrowselst);

    What i don't understand is the information being returned and how to turn it into individual files for use in php.

    Anybody out there been playing around with this??

    Cheers
    Trying deperately to understand
    actionscript

  2. #2
    Monkey Moderator Lexicon's Avatar
    Join Date
    Jul 2001
    Location
    UK
    Posts
    2,038
    A multiple file upload is a bunch of single file uploads.
    So with the FileReferenceList you get from the dialog box, you basically iterate through that list and initiate a file upload as normal for each one...

    Code:
    import flash.net.FileReferenceList;
    import flash.net.FileReference;
    
    var fileUploadmulti:Object = new Object();
    var fileUploadsingle:Object = new Object();
    
    fileUploadmulti.onSelect = function(fileRefList:FileReferenceList)
    {
    	var list:Array = fileRefList.fileList;
    	for (var i:Number = 0; i<list.length; i++)
    	{
    		trace("UPLOADING FILE: "+list[i].name);
    		list[i].addListener(fileUploadsingle);
    		list[i].upload("http://www.somewhere.com/");
    	}
    };
    
    fileUploadmulti.onCancel = function():Void 
    {
    	trace("onCancel");
    };
    
    fileUploadmulti.onOpen = function(file:FileReference):Void 
    {
    	trace("onOpen: "+file.name);
    };
    
    fileUploadsingle.onProgress = function(file:FileReference, bytesLoaded:Number, bytesTotal:Number):Void 
    {
    	trace(file.name+" onProgress with bytesLoaded: "+bytesLoaded+" bytesTotal: "+bytesTotal);
    };
    
    fileUploadsingle.onComplete = function(file:FileReference):Void 
    {
    	trace("onComplete: "+file.name);
    };
    
    fileUploadsingle.onHTTPError = function(file:FileReference, httpError:Number):Void 
    {
    	trace("onHTTPError: "+file.name+" httpError: "+httpError);
    };
    
    fileUploadsingle.onIOError = function(file:FileReference):Void 
    {
    	trace("onIOError: "+file.name);
    };
    
    fileUploadsingle.onSecurityError = function(file:FileReference, errorString:String):Void 
    {
    	trace("onSecurityError: "+file.name+" errorString: "+errorString);
    };
    
    var fileRefList:FileReferenceList = new FileReferenceList();
    fileRefList.addListener(fileUploadmulti);
    fileRefList.browse();
    www.lexicon-design.co.uk
    If we aren't supposed to eat animals, then why are they made of meat?
    If Vegetarians like animals so much, why do they eat all their food?

  3. #3
    Senior Member
    Join Date
    Apr 2004
    Posts
    111
    Thank You Lexicon
    Trying deperately to understand
    actionscript

  4. #4
    Monkey Moderator Lexicon's Avatar
    Join Date
    Jul 2001
    Location
    UK
    Posts
    2,038
    no worries, the example is basically the same as the flash help files for FileReferenceList so it's worth looking through. I just added an extra listener object to make it easier to understand the bit dealing with multiple files and the bit dealing with single file uploads.
    www.lexicon-design.co.uk
    If we aren't supposed to eat animals, then why are they made of meat?
    If Vegetarians like animals so much, why do they eat all their food?

  5. #5
    Senior Member
    Join Date
    Apr 2004
    Posts
    111
    I just tried it and it works great ... and for what i'm creating it's the best thing thats ever happened in the web world

    Can i ask just one last thing though ... on the list[i].upload ... how do i pass additional variables to my php file ???

    Many thanks again.
    Trying deperately to understand
    actionscript

  6. #6
    Monkey Moderator Lexicon's Avatar
    Join Date
    Jul 2001
    Location
    UK
    Posts
    2,038
    you can append it to the script string...

    I have done that and it works. I released my first flash8 commercial app yesterday with that method

    list[i].upload("http://www.somewhere.com?myUpload.php?myVar="+myVar);
    www.lexicon-design.co.uk
    If we aren't supposed to eat animals, then why are they made of meat?
    If Vegetarians like animals so much, why do they eat all their food?

  7. #7
    Senior Member
    Join Date
    Apr 2004
    Posts
    111
    Nice one ... i bet it made a difference

    Thanks Again Lexicon
    Trying deperately to understand
    actionscript

  8. #8
    Senior Member
    Join Date
    Apr 2004
    Posts
    111
    Sorry lexicon to ask again ... but can i just use sendAndLoad and pass the list that way?
    Trying deperately to understand
    actionscript

  9. #9
    Monkey Moderator Lexicon's Avatar
    Join Date
    Jul 2001
    Location
    UK
    Posts
    2,038
    That wouldn't work because LoadVars does not have access to the FileReference.upload function and so would not initiate a file upload. It would probably just send [object object] as a string.
    www.lexicon-design.co.uk
    If we aren't supposed to eat animals, then why are they made of meat?
    If Vegetarians like animals so much, why do they eat all their food?

  10. #10
    Senior Member
    Join Date
    Apr 2004
    Posts
    111
    I know i'm being stupid here ... but i can't get the vars into php.

    My code is this in swf:
    list[i].upload("/upload/fileUpload.php?var1="+var1);

    and my php is this:
    <?php
    $var1 = $_POST['var1'];
    $moveToDir = 'images/';
    foreach ($_FILES as $fieldName => $file) {
    $uploadFileName = $file['name'];
    $content1 = str_replace(" ", "_", $uploadFileName);
    $content = $var1.'_'.$content1;
    move_uploaded_file($file['tmp_name'], $moveToDir.$content);
    }
    ?>
    Trying deperately to understand
    actionscript

  11. #11
    Monkey Moderator Lexicon's Avatar
    Join Date
    Jul 2001
    Location
    UK
    Posts
    2,038
    I'm not really a huge PHP man so I might be talking crap here.....

    But try using var1 straight away without assigning it to the post var...
    The variable is not sent via POST.

    Code:
    <?php
        $moveToDir = 'images/';
        foreach ($_FILES as $fieldName => $file) {
            $uploadFileName = $file['name'];
            $content1 = str_replace(" ", "_", $uploadFileName);
            $content = $var1.'_'.$content1;
            move_uploaded_file($file['tmp_name'], $moveToDir.$content);
        }
    ?>
    Thats what I've done in the past (infact on that last project thats how it works).
    www.lexicon-design.co.uk
    If we aren't supposed to eat animals, then why are they made of meat?
    If Vegetarians like animals so much, why do they eat all their food?

  12. #12
    Senior Member
    Join Date
    Apr 2004
    Posts
    111
    Yep ... right as usual ... thanks dude. Sorry for being a pain.

    Cheers
    Trying deperately to understand
    actionscript

  13. #13
    Monkey Moderator Lexicon's Avatar
    Join Date
    Jul 2001
    Location
    UK
    Posts
    2,038
    always a pleasure
    www.lexicon-design.co.uk
    If we aren't supposed to eat animals, then why are they made of meat?
    If Vegetarians like animals so much, why do they eat all their food?

  14. #14
    Senior Member
    Join Date
    Apr 2004
    Posts
    111
    Everything is working great ... just one last thing (ha ha) ... can i receive a variable back from .upload() like i do with sendAndLoad, i.e. list[i].onLoad = results; ??

    Cheers
    Trying deperately to understand
    actionscript

  15. #15
    Monkey Moderator Lexicon's Avatar
    Join Date
    Jul 2001
    Location
    UK
    Posts
    2,038
    I don't believe so, but I could be wrong.
    www.lexicon-design.co.uk
    If we aren't supposed to eat animals, then why are they made of meat?
    If Vegetarians like animals so much, why do they eat all their food?

  16. #16
    I've been using coldfusion for my uploads. When the upload is complete CF sends a response header back to flash with the name of the files, size, etc. But I haven't found a way to modify that reponse header.

    code:

    listener.onComplete = function(response){
    for(var name in response){
    trace(name+": "+response[name]);
    }
    }


  17. #17
    Member
    Join Date
    Mar 2001
    Posts
    78
    I have the above Filereferencelist code working as is. However, I would like the upload to be based on a button click. I'm getting "undefined" when trying to reference the list from the onclick function that is triggered by the button listener. Is there anything in particular I'm doing wrong? Do I need to declare the filereferencelist and list as a timeline or global var?

  18. #18
    Monkey Moderator Lexicon's Avatar
    Join Date
    Jul 2001
    Location
    UK
    Posts
    2,038
    You need to declare the list you are storing the fileReferences in outside of the onSelect function so that you can access it from other functions.

    e.g. replace this function
    Code:
    fileUploadmulti.onSelect = function(fileRefList:FileReferenceList)
    {
    	var list:Array = fileRefList.fileList;
    	for (var i:Number = 0; i<list.length; i++)
    	{
    		trace("UPLOADING FILE: "+list[i].name);
    		list[i].addListener(fileUploadsingle);
    		list[i].upload("http://www.somewhere.com/");
    	}
    };
    with something like this...

    Code:
    var list:Array;
    
    fileUploadmulti.onSelect = function(fileRefList:FileReferenceList)
    {
    	list = fileRefList.fileList;
    };
    
    _btn.onRelease = function()
    {
    	for (var i:Number = 0; i<list.length; i++)
    	{
    		trace("UPLOADING FILE: "+list[i].name);
    		list[i].addListener(fileUploadsingle);
    		list[i].upload("http://www.somewhere.com/");
    	}
    }
    www.lexicon-design.co.uk
    If we aren't supposed to eat animals, then why are they made of meat?
    If Vegetarians like animals so much, why do they eat all their food?

  19. #19
    Member
    Join Date
    Mar 2001
    Posts
    78
    Perfect. Thank you much. It's working now.

    Just to finish it off, for those that might actually need assistance with this:

    To stop the browse from happening on load and have it from a "browse" button, just drop the fileRefList.browse(); into a onRelease-triggered function for the button.

    browseBtn.onRelease= function() {
    fileRefList.browse();
    }

  20. #20
    Junior Member
    Join Date
    May 2001
    Posts
    22
    This post has proved very helpfull.

    One question. As far as I can tell using fileReferenceList.browse() allows you to only select multiple files from within the same local directory. Maybe I'm wrong.?

    If this is the case how would one set up a system where fileReferenceList.browse() could be called multiple times to allow someone to add files to the .filestList property without over writing what it already contains?

    Thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center