swfupload: how to parse returned data from upload.php script?

2k Views Asked by At

Googled for about 1 hour, still cannot find how to perform this simple task!

upload.php ends up with:

    echo "FILEID:" . $randomid; // Return the file id to the script

now trying to get this FILEID:

          function uploadSuccess(serverData) {
        alert(serverData);
      }

Alerts [object Object]. Documentation does not help at all. No examples are available.

Please help!

3

There are 3 best solutions below

0
Alex G On BEST ANSWER

SOLVED! It was much more simple. Why do I always think complicated? function uploadSuccess(fileObject, serverData, response) { alert(serverData); }

3
Eric On

Well, for one thing, I think you have your uploadSuccess input params wrong. Documentation states: uploadSuccess(file object, server data, received response) is the correct signature.

So for starters, you need to throw some extra params on your success callback :) Second, serverData is whatever your server output. In your example, it looks like the response is going to be { "FILEID" : "somerandomid" } (I'm assuming it's coming back as JSON, though perhaps that's a misguided assumption)

In any case, to access the randomly created id, you should simply have to do

function uploadSuccess(fileObject, serverData, response){
    alert(serverData.FILEID); 
 }

Also, you should use Firebug, which is a plugin for Firefox. It will let you place a breakpoint on your callback function and let you interrogate serverData to see exactly what's in it.

1
eduardomozart On

I have used JSON with SWFUpload. The problem to read FILEID (@Eric suggestion) is that SWFUpload returns file name from the uploaded file (per example, client have uploaded "test.txt" to the server, this is the name of the file it returns). It is ok, but if you have a PHP Script that rename files (to avoid replacing), then the name of file on server is not "test.txt" if this file exists, but "test(2).txt".

In uploadSuccess, i have used:

.on('uploadSuccess', function(event, file, serverData){
    var responseData = jQuery.parseJSON(serverData);
    var upload_url_tmp = responseData["name"];
    [...]

On Server-Side, I have used:

$data = array('name' => $file);
    echo json_encode($data);

Please note the $file var is the name of the file saved on Server (after rename). Worked with PHP 5.4.9 and SWFUpload 2.2.0.1