Redirect in Plupload

434 views Asked by At

Wonder if someone can advise please - I am using Plupload and need to redirect to a different page to add information into a database.

I can upload the file perfectly, but I cannot seem to get it to redirect.

Ive tried putting in the code into "UploadComplete" and in a different test into "FileUploaded"

Using Firebug I put in breaks into the script and both sections are stopped at at the start, but when the file is finished it doesnt touch either of them.

Therefore I thought Id put it into the upload.php file in the Chunks section (as per a google result suggested).

// Check if file has been uploaded
if (!$chunks || $chunk == $chunks - 1) {
// Strip the temp .part suffix off 
rename("{$filePath}.part", $filePath);
$mypi = $projid;
header("Location: addimg.php?file=" . $fileName . "&blid=" . $mypi);
die();
}

In debugging it went through and hit the redirect and the die following, then then it went back to the start of the "if (!chunks etc)" and run through it again, but never redirected.

What am I missing please! Im pulling out my hair to make this work.

Thanks in advance

Javascript:

var uploader = new plupload.Uploader({
browse_button: 'browse', // this can be an id of a DOM element or the DOM element itself
container: document.getElementById('upcontainer'), // ... or DOM Element itself

//url: 'upload.php',
 url: 'uploader_plup/upload.php?wts=<?php echo $projid;?>',

chunk_size: '2mb',

max_retries: '5',      

// Sort files
sortable: true,

// Specify what files to browse for
    filters : {
        mime_types:[
        {title : "Image files", extensions : "jpg,gif,png"},
        {title : "Zip files", extensions : "zip"},
        {title : "Video files", extensions : "mov,mp4,mpeg,avi,mpg,wmv,w4v,flv,ogv,divx,asf,asx,ram,m2v"},                                                      
        {title : "PDF files", extensions : "pdf"},                                 
        {title : "Text files", extensions : "htm,html,doc,txt,rtf,ppt"}
    ],},
}
);

uploader.init();



uploader.bind('FilesAdded', function(up, files) {
var html = '';
plupload.each(files, function(file) {
    html += '<li id="' + file.id + '">' + file.name + '     ('+plupload.formatSize(file.size) + ') <b></b></li>';
});
document.getElementById('filelist').innerHTML += html;
});

uploader.bind('FileUploaded', function(up, file, info) {
            // Called when file has finished uploading
            log('[FileUploaded] File:', file, "Info:", info);
        }); 

uploader.bind('ChunkUploaded', function(up, file, info) {
   // Called when file chunk has finished uploading
  log('[ChunkUploaded] File:', file, "Info:", info);
});

uploader.bind('UploadProgress', function(up, file) {
document.getElementById(file.id).getElementsByTagName('b')[0].innerHTML = '<span>' + file.percent + "%</span>";
log('[UploadProgress] File:', file, "Info:", info);
});

uploader.bind('UploadComplete', function(up, files) {
            // Called when file has finished uploading 
            // console.log("[UploadComplete]");
            // backButtonState = true;
            var totup = uploader.total.uploaded + 1;
            var fillen =  uploader.files.length;
             if(totup == fillen)
            {
                var mypi = "<?php  echo $projid; ?>";
                window.location = "addimg.php?file="+files+"&blid="+mypi;   
            }
           log('[UploadComplete]');   

        });

uploader.bind('Error', function(up, err) {
document.getElementById('console').innerHTML += "\nError #" + err.code +   ": " + err.message;
log('[Error] File:', file, "Info:", err.message);
});

document.getElementById('start-upload').onclick = function() {
uploader.start();       
};

function log() {
    var str = "";

    plupload.each(arguments, function(arg) {
        var row = "";

        if (typeof(arg) != "string") {
            plupload.each(arg, function(value, key) {
                // Convert items in File objects to human readable form
                if (arg instanceof plupload.File) {
                    // Convert status to human readable
                    switch (value) {
                        case plupload.QUEUED:
                            value = 'QUEUED';
                            break;

                        case plupload.UPLOADING:
                            value = 'UPLOADING';
                            break;

                        case plupload.FAILED:
                            value = 'FAILED';
                            break;

                        case plupload.DONE:
                            value = 'DONE';
                            break;
                    }
                }

                if (typeof(value) != "function") {
                    row += (row ? ', ' : '') + key + '=' + value;
                }
            });

            str += row + " ";
        } else {
            str += arg + " ";
        }
    });

    var log = document.getElementById('console');
    log.innerHTML += str + "\n";
}


</script> 
1

There are 1 answers

2
Jonatas Walker On

Try this way:

uploader.bind('UploadComplete', function(up, files) {
    // Called when all files are either uploaded or failed

    var mypi = "<?php  echo $projid; ?>";
    window.location = "addimg.php?file="+files+"&blid="+mypi;   

    /* This is not necessary since UploadComplete means all is done! 
    var totup = uploader.total.uploaded + 1;
    var fillen =  uploader.files.length;
    if(totup == fillen) {}
    log('[UploadComplete]');*/

});

And remove this php line:

header("Location: addimg.php?file=" . $fileName . "&blid=" . $mypi);