Actionscript multiple file upload, with parameter passing is not working

Posted by Metropolis on Stack Overflow See other posts from Stack Overflow or by Metropolis
Published on 2010-04-07T18:49:10Z Indexed on 2010/04/07 18:53 UTC
Read the original article Hit count: 448

Filed under:
|
|

Hey everyone,

First off, I am very bad at flash/actionscript, it is not my main programming language.

I have created my own file upload flash app that has been working great for me up until this point. It uses PHP to upload the files and sends back a status message which gets displayed in a status box to the user.

Now I have run into a situation where I need the HTML to pass a parameter to the Actionscript, and then to the PHP file using POST. I have tried to set this up just like adobe has it on http://livedocs.adobe.com/flex/3/html/help.html?content=17_Networking_and_communications_7.html without success.

Here is my Actionscript code

import fl.controls.TextArea; 

//Set filters
var imageTypes:FileFilter = new FileFilter("Images (*.jpg, *.jpeg, *.gif, *.png)", "*.jpg; *.jpeg; *.gif; *.png");
var textTypes:FileFilter = new FileFilter("Documents (*.txt, *.rtf, *.pdf, *.doc)", "*.txt; *.rtf; *.pdf; *.doc");
var allTypes:Array = new Array(textTypes, imageTypes);
var fileRefList:FileReferenceList = new FileReferenceList();

//Add event listeners for its various fileRefList functions below
upload_buttn.addEventListener(MouseEvent.CLICK, browseBox);
fileRefList.addEventListener(Event.SELECT, selectHandler);


function browseBox(event:MouseEvent):void {
    fileRefList.browse(allTypes);
}

function selectHandler(event:Event):void {
    var phpRequest:URLRequest = new URLRequest("ajax/upload.ajax.php");
    var flashVars:URLVariables = objectToURLVariables(this.root.loaderInfo);
    phpRequest.method = URLRequestMethod.POST;
    phpRequest.data = flashVars;
    var file:FileReference;
    var files:FileReferenceList = FileReferenceList(event.target);
    var selectedFileArray:Array = files.fileList;
    var listener:Object = new Object();

    for (var i:uint = 0; i < selectedFileArray.length; i++) {
        file = FileReference(selectedFileArray[i]);

        try { 
            file.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, phpResponse);
            file.upload(phpRequest);
        }
        catch (error:Error) { 
            status_txt.text = file.name + " Was not uploaded correctly (" + error.message + ")"; 
        }
}
}

function phpResponse(event:DataEvent):void {    
    var file:FileReference = FileReference(event.target);
    status_txt.htmlText += event.data;
}

function objectToURLVariables(parameters:Object):URLVariables {
    var paramsToSend:URLVariables = new URLVariables();
    for(var i:String in parameters) {
        if(i!=null) {
            if(parameters[i] is Array) paramsToSend[i] = parameters[i];
            else paramsToSend[i] = parameters[i].toString();
        }
}

    return paramsToSend;
}

The flashVars variable is the one that should contain the values from the HTML file. But whenever I run the program and output the variables in the PHP file I receive the following.

//Using this command on the PHP page
print_r($_POST);

//I get this for output
Array
(
    [Filename] => testfile.txt
    [Upload] => Submit Query
)

Its almost like the parameters are getting over written or are just not working at all.

Thanks for any help, Metropolis

© Stack Overflow or respective owner

Related posts about actionscript3

Related posts about php