Skip to main content
Copy edited. Removed meta information.
Source Link
Peter Mortensen
  • 31.3k
  • 22
  • 109
  • 132

I have been using the below script to upload images which happens to workswork fine. Hope this will help you too.

#HTML #JS 

#JavaScript

jQuery('document').ready(function(){
    
        var input = document.getElementById("file");
        var formdata = false;
        if (window.FormData) {
            formdata = new FormData();
        }
        input.addEventListener("change", function (evt) {
        
            var i = 0, len = this.files.length, img, reader, file;
    
            for ( ; i < len; i++ ) {
                file = this.files[i];
    
                if (!!file.type.match(/image.*/)) {
                    if ( window.FileReader ) {
                        reader = new FileReader();
                        reader.onloadend = function (e) { 
                            //showUploadedItem(e.target.result, file.fileName);
                        };
                        reader.readAsDataURL(file);
                    }
                
                    if (formdata) {
                        formdata.append("image", file);
                        formdata.append("extra",'extra-data');
                    }
                
                    if (formdata) {
                        jQuery('div#response').html('<br /><img src="ajax-loader.gif"/>');
                    
                     
                        jQuery.ajax({
                            url: "upload.php",
                            type: "POST",
                            data: formdata,
                            processData: false,
                            contentType: false,
                            success: function (res) {
                             jQuery('div#response').html("Successfully uploaded");
                            }
                        });
                    }
                }
                else
                {
                    alert('Not a vaild image!');
                }   
            }
    
        }, false);
 
    });

Here you are not using a plugin and stuff. You can change the code as you want. You are not blindly coding here. This is the core functionality of any jQuery file upload. Actually Javascript.

Hope this helps.

I have been using below script to upload images which happens to works fine. Hope this will help you too.

#HTML #JS

jQuery('document').ready(function(){
    
        var input = document.getElementById("file");
        var formdata = false;
        if (window.FormData) {
            formdata = new FormData();
        }
        input.addEventListener("change", function (evt) {
        
            var i = 0, len = this.files.length, img, reader, file;
    
            for ( ; i < len; i++ ) {
                file = this.files[i];
    
                if (!!file.type.match(/image.*/)) {
                    if ( window.FileReader ) {
                        reader = new FileReader();
                        reader.onloadend = function (e) { 
                            //showUploadedItem(e.target.result, file.fileName);
                        };
                        reader.readAsDataURL(file);
                    }
                
                    if (formdata) {
                        formdata.append("image", file);
                        formdata.append("extra",'extra-data');
                    }
                
                    if (formdata) {
                        jQuery('div#response').html('<br /><img src="ajax-loader.gif"/>');
                    
                     
                        jQuery.ajax({
                            url: "upload.php",
                            type: "POST",
                            data: formdata,
                            processData: false,
                            contentType: false,
                            success: function (res) {
                             jQuery('div#response').html("Successfully uploaded");
                            }
                        });
                    }
                }
                else
                {
                    alert('Not a vaild image!');
                }   
            }
    
        }, false);
 
    });

Here you are not using a plugin and stuff. You can change the code as you want. You are not blindly coding here. This is the core functionality of any jQuery file upload. Actually Javascript.

Hope this helps.

I have been using the below script to upload images which happens to work fine.

#HTML 

#JavaScript

jQuery('document').ready(function(){
    var input = document.getElementById("file");
    var formdata = false;
    if (window.FormData) {
        formdata = new FormData();
    }
    input.addEventListener("change", function (evt) {
        var i = 0, len = this.files.length, img, reader, file;

        for ( ; i < len; i++ ) {
            file = this.files[i];

            if (!!file.type.match(/image.*/)) {
                if ( window.FileReader ) {
                    reader = new FileReader();
                    reader.onloadend = function (e) {
                        //showUploadedItem(e.target.result, file.fileName);
                    };
                    reader.readAsDataURL(file);
                }

                if (formdata) {
                    formdata.append("image", file);
                    formdata.append("extra",'extra-data');
                }

                if (formdata) {
                    jQuery('div#response').html('<br /><img src="ajax-loader.gif"/>');

                    jQuery.ajax({
                        url: "upload.php",
                        type: "POST",
                        data: formdata,
                        processData: false,
                        contentType: false,
                        success: function (res) {
                         jQuery('div#response').html("Successfully uploaded");
                        }
                    });
                }
            }
            else
            {
                alert('Not a vaild image!');
            }
        }

    }, false);
});

Here you are not using a plugin and stuff. You can change the code as you want. You are not blindly coding here. This is the core functionality of any jQuery file upload. Actually Javascript.

Source Link
Techie
  • 45k
  • 44
  • 161
  • 246

I have been using below script to upload images which happens to works fine. Hope this will help you too.

#HTML #JS

jQuery('document').ready(function(){
    
        var input = document.getElementById("file");
        var formdata = false;
        if (window.FormData) {
            formdata = new FormData();
        }
        input.addEventListener("change", function (evt) {
        
            var i = 0, len = this.files.length, img, reader, file;
    
            for ( ; i < len; i++ ) {
                file = this.files[i];
    
                if (!!file.type.match(/image.*/)) {
                    if ( window.FileReader ) {
                        reader = new FileReader();
                        reader.onloadend = function (e) { 
                            //showUploadedItem(e.target.result, file.fileName);
                        };
                        reader.readAsDataURL(file);
                    }
                
                    if (formdata) {
                        formdata.append("image", file);
                        formdata.append("extra",'extra-data');
                    }
                
                    if (formdata) {
                        jQuery('div#response').html('<br /><img src="ajax-loader.gif"/>');
                    
                     
                        jQuery.ajax({
                            url: "upload.php",
                            type: "POST",
                            data: formdata,
                            processData: false,
                            contentType: false,
                            success: function (res) {
                             jQuery('div#response').html("Successfully uploaded");
                            }
                        });
                    }
                }
                else
                {
                    alert('Not a vaild image!');
                }   
            }
    
        }, false);

    });

#Explanation

I use response div to show the uploading animation and response after upload is done.

Best part is you can send extra data such as ids & etc with the file when you use this script. I have mention it extra-data as in the script.

At the PHP level this will work as normal file upload. extra-data can be retrieved as $_POST data.

Here you are not using a plugin and stuff. You can change the code as you want. You are not blindly coding here. This is the core functionality of any jQuery file upload. Actually Javascript.

Hope this helps.