0

For some reason my script for file upload is not working correctly and I have absolutely no idea why :? The script manages to send the file/s but I can not track the progress or state changes. I've tried to comment out unecessary bits for debugging but it still does not work.

If anyone can point out my mistake I will be forever grateful and give you a virtual cookie

$('#submit').click(function(e){
            e.preventDefault();
            $('#progressContainer').slideDown(10);
            var f = document.getElementById('file'),
                pb = document.getElementById('pb'),
                pt = document.getElementById('pt');

            app.uploader({
                files:f,
                progressBar:pb,
                progressText:pt,
                processor:'scripts/php/upload.php',

                finished: function(){
                    $('#pt').html("Upload complete");
                },
                error: function(){
                    $('#pt').html("Upload Error, please try again");
                }
            });
        });

var app = app || {};

(function(o){
    "use strict"

    var ajax, getFormData, setProgress;

    ajax = function(data){
        var xmlhttp = new XMLHttpRequest(), uploaded;
        xmlhttp.addEventListener('readystatechange', function(){
            if(this.readystate === 4){
                if(this.status === 200){
                    //uploaded = JSON.parse(this.response);
                    //if(typeof o.options.finished === 'function'){
                        o.options.finished();
                    //}
                } else {
                    //if(typeof o.options.error === 'function'){
                        o.options.error();
                    //} 
                }   
            }
        });
        xmlhttp.upload.addEventListener('progress', function(event){
            var percent;
            //if(event.lengthComputable === true){
                percent = math.round(event.loaded / event.total * 100);
                setProgress(percent);
                alert(percent)
            //}
        });
        xmlhttp.open('POST', o.options.processor);
        xmlhttp.send(data);
    }
    getFormData = function(src){
        var data = new FormData(), i;
        for(i=0;i<src.length;i++){
            data.append('file[]',src[i]);
        }
        data.append('formSent',true);
        return data;
    }
    setProgress = function(val){
        $('#pb').animate({width:val+"%"},10);
        $('#pt').html(val+"%");
    }

    o.uploader = function(opt){
        o.options = opt;
        if(o.options.files !== undefined){
            ajax(getFormData(o.options.files.files));
        }
    }
}(app));
Jonathan H
  • 141
  • 1
  • 12

3 Answers3

1

Try this:

xmlhttp.addEventListener('readystatechange', function(e){
        if(e.currentTarget.readyState === 4){
            if(e.currentTarget.status === 200){
               o.options.finished();
            } else {
               o.options.error();
            }   
        }
    });
Kaushik Makwana
  • 2,422
  • 9
  • 31
  • 50
  • 1
    This is definitely the best answer! All of those "inline" notations onreadystatechange = function() above in fact self-contain one big disadvantage: "this" in their body always belongs to XmlHttpRequest object and therefore you will never be able to use "this" to refer e.g. your class members as it is always being masked with XmlHttpRequest "this" ... – Petr Pivonka Aug 25 '22 at 20:58
0

Try changing this:

xmlhttp.addEventListener('readystatechange', function(){
        if(this.readystate === 4){
            if(this.status === 200){
                //uploaded = JSON.parse(this.response);
                //if(typeof o.options.finished === 'function'){
                    o.options.finished();
                //}
            } else {
                //if(typeof o.options.error === 'function'){
                    o.options.error();
                //} 
            }   
        }
    });

To this:

xmlhttp.onreadystatechange = function() {
        if(xmlhttp.readystate === 4){
            if(xmlhttp.status === 200){
                //uploaded = JSON.parse(this.response);
                //if(typeof o.options.finished === 'function'){
                    o.options.finished();
                //}
            } else {
                //if(typeof o.options.error === 'function'){
                    o.options.error();
                //} 
            }   
        }
    });
  • Still is not working :| I've even put an alert() in the status if and else condition but even the alerts are not showing – Jonathan H Apr 08 '16 at 19:34
0

I used 'math.round()' instead of 'Math.round()'. A very simple error but it was stopping the rest of the script from executing. Thanks Marvin Medeiros for the sound advice of always checking the error console

var app = app || {};

(function(o){
    "use strict"

    var ajax, getFormData, setProgress;

    ajax = function(data){
        var xmlhttp = new XMLHttpRequest(), uploaded;
        xmlhttp.addEventListener('onreadystatechange', function(){
            if(xmlhttp.readystate === 4){
                if(xmlhttp.status === 200){
                    uploaded = JSON.parse(this.response);
                    if(typeof o.options.finished === 'function'){
                    o.options.finished();
                    }
                } else {
                    if(typeof o.options.error === 'function'){
                    o.options.error();
                    } 
                }   
            }
        });
        xmlhttp.upload.addEventListener('progress', function(event){
            var percent;
            if(event.lengthComputable === true){
                percent = Math.round(event.loaded / event.total * 100);
                setProgress(percent);
            }
        });
        xmlhttp.open('POST', o.options.processor);
        xmlhttp.send(data);
    }
    getFormData = function(src){
        var data = new FormData(), i;
        for(i=0;i<src.length;i++){
            data.append('file[]',src[i]);
        }
        data.append('formSent',true);
        return data;
    }
    setProgress = function(val){
        $('#pb').animate({width:val+"%"},10);
        $('#pt').html(val+"%");
    }

    o.uploader = function(opt){
        o.options = opt;
        if(o.options.files !== undefined){
            ajax(getFormData(o.options.files.files));
        }
    }
}(app));
Jonathan H
  • 141
  • 1
  • 12