views:

317

answers:

3

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

A: 

Try...

print_r($_FILES);
Brant
A: 

Like I said in my comment: Do you successfully receive the variable in Flash from the flashvars?

I haven't done Flash in a while but maybe, instead of your objectToURLVariables function, just referencing each variable directly is a better way. At least to figure out if you have those variables from your HTML page. So maybe do something like this:

var myVar:String = LoaderInfo(this.root.loaderInfo).parameters.myVar;
var flashVars:URLVariables = objectToURLVariables(myVar);
Abs
A: 

Ok, I have fixed the issue somehow.....I kept changing things back and forth and realized that the cache had not been cleared in awhile. I cleared the cache and it started working for some reason.

I did change one line back to the way I had it before.

I changed
var flashVars:URLVariables = objectToURLVariables(this.root.loaderInfo);

To
var flashVars:URLVariables = objectToURLVariables(root.loaderInfo.parameters);

Im not positive that this was causing the problem. It may have been that I just needed to clear the cache the whole time. Anyway, thanks for your help guys.

Metropolis