views:

134

answers:

1

After long time of searching the best upload technique I've decided to go with jQuery Uploadify.

I got no problem implementing it into existing code, excluding one thing. I created strict upload validation rules.

The main problem is that onAllComplete() function returns success message. In my validation file I tried to use echo 'errror' and die('error') as well. Without any luck. I've searched Google and SO. Without any luck.

Do you have any ideas/advices/snippets? Any help appreciated.

Update
To make my situation clear. I'm using onComplete() function to show eventual but I have no idea how to force onAllComplete() function to show correct data.

Update 2 I applied IgalSt (I'm sure I did something similar before) but still having the same issue. My onComplete function looks as following now:

onComplete: function (ev, queueID, fileObj, response) {
    var result = $.parseJSON(response).success;
    alert(result);
            if (result == false) {
        alert('invalid')
    } else {
        alert('everything is OK');
        var size = Math.round(c.size/1024);
    }
    return false;
},

If I alert(response) it alertsfalse` so it should be working. Any suggestions why it's not? onAllComplete() function still shows this file as uploaded successfully.

Regards, Tom

+1  A: 

as long as the server returns 200 state header you the onAllComplete callback will fire. onError callback will fire only when you've got an error header back: 500 or 404 for example. Therefore, your server side code should return a response with a flag if there was a problem and you should use onAllComple to do some kind of IF for than.

For example if you page return a JSON with error:

[{"success":"false"}]

you onAllComplete function will look something like that:

'onComplete':   function(ev, queueID, fileObj, response) {
            var result = $.parseJSON(response).success;
            if (!result)
              alert('invalid')
                        else
                           alert('everything is OK');
        }
IgalSt
I missed that part about headers while reading documentation. You meant onComplete() not onAllComplete(), right? Anyway, I did something similar before and still showing file as completed (even if I put only `echo json_encode(array('success' => 'false'));` in my upload file. Wondering why. Have no idea, really. According to my knowledge it should be working. Any ideas?
Tom
the difference between onComplete and onAllComplete is that onComplete is being fired every time you finish uploading a file and onAllComplete will be fired when you've finished uploading all the files (if you have got 5 files onComplete will be fired 5 times while onAllComplete will be fired once).As long as your php code doesn't have any 500 error (and exists on the server), you will not go into onError callback. You have to do an IF inside the onComplete or onAllComplete (which better works for you) for case that success is falce (line in my example)
IgalSt
@Pekka - Of course I did..but I'm a bit tired today (after all night testing APC, uploadprogress) and I forgot to...uncomment this part. Delicious. @lgalSt - I just forgot to uncomment header into my code. Wondering if there's any way to edit an error message shown in the file block? Currently it's showing `HTTP Error`. **Edit** I can swear I saw comment posted by Pekka before :D
Tom
It's ok :)Anyway, I think that you better not use the error header as this is not the purpose of it (as much as I know). You better return a flag of success of failure from your php script.
IgalSt
@lgalSt - `You better return a flag of success of failure from your php script.` Could you be so nice and describe what do you mean? If I;m not using error header it shows file as uploaded :D
Tom
before saving the file on the server with php you validate it, right? So if it failed validation you return success=false (JSON, XML whatever you like). If the file is valid return success=true (and save the file to the disk). In onComplete check the response and and do whatever you like to do in case of success and in case of failure.
IgalSt
How I see your point of view. But how to avoid unsuccessfull uploading being shown are successfull?
Tom
if (!$.parseJSON(response).success) { /*put here your code that handles case of "unsuccessful upload"*/} else { /*put here your code that handles case of "successful" upload */}
IgalSt
Yeah, I know that. I meant... how to delete progress bar from the list, etc. :)
Tom
Check the documentation:http://www.uploadify.com/documentation/ and the uploadifyCancel method
IgalSt
Thanks for your help. Anyway, due to weird errors I've been forced to switch to SWFUpload :)
Tom