Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

ajax uploading progress

<script>
$(document).ready(function(){
    // File upload via Ajax
    $("#uploadForm").on('submit', function(e){
        e.preventDefault();
        $.ajax({
            xhr: function() {
                var xhr = new window.XMLHttpRequest();
                xhr.upload.addEventListener("progress", function(evt) {
                    if (evt.lengthComputable) {
                        var percentComplete = ((evt.loaded / evt.total) * 100);
                        $(".progress-bar").width(percentComplete + '%');
                        $(".progress-bar").html(percentComplete+'%');
                    }
                }, false);
                return xhr;
            },
            type: 'POST',
            url: 'upload.php',
            data: new FormData(this),
            contentType: false,
            cache: false,
            processData:false,
            beforeSend: function(){
                $(".progress-bar").width('0%');
                $('#uploadStatus').html('<img src="images/loading.gif"/>');
            },
            error:function(){
                $('#uploadStatus').html('<p style="color:#EA4335;">File upload failed, please try again.</p>');
            },
            success: function(resp){
                if(resp == 'ok'){
                    $('#uploadForm')[0].reset();
                    $('#uploadStatus').html('<p style="color:#28A74B;">File has uploaded successfully!</p>');
                }else if(resp == 'err'){
                    $('#uploadStatus').html('<p style="color:#EA4335;">Please select a valid file to upload.</p>');
                }
            }
        });
    });
	
    // File type validation
    $("#fileInput").change(function(){
        var allowedTypes = ['application/pdf', 'application/msword', 'application/vnd.ms-office', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'image/jpeg', 'image/png', 'image/jpg', 'image/gif'];
        var file = this.files[0];
        var fileType = file.type;
        if(!allowedTypes.includes(fileType)){
            alert('Please select a valid file (PDF/DOC/DOCX/JPEG/JPG/PNG/GIF).');
            $("#fileInput").val('');
            return false;
        }
    });
});
</script>
Comment

PREVIOUS NEXT
Code Example
Javascript :: get buffer from file javascript 
Javascript :: make ajax calls with jQuery 
Javascript :: react native linking email 
Javascript :: express bodyparser deprecated 
Javascript :: router navigatebyurl 
Javascript :: vue.js function to always uppercase when the client input lowercase 
Javascript :: console log object js 
Javascript :: how to update a json file javascript 
Javascript :: route parammap subscribe angular 9 
Javascript :: redirect with react router v6 
Javascript :: javascript get random line from text file 
Javascript :: how to get value from input field in javascript 
Javascript :: encode in javascript 
Javascript :: javascript object destructuring rename 
Javascript :: jquery is check 
Javascript :: Add event listener for loop 
Javascript :: java password regex 
Javascript :: mongoose update createdAt 
Javascript :: flutter parse json 
Javascript :: Uncaught (in promise): NullInjectorError 
Javascript :: react flip move 
Javascript :: js poll dom 
Javascript :: nested shorthand if javascript 
Javascript :: js get date in ms 
Javascript :: video preview javascript 
Javascript :: javascript download csv 
Javascript :: show ajax error wordpress 
Javascript :: nodejs json beautify 
Javascript :: jquery validate checkbox before submit 
Javascript :: nodejs express hot reload 
ADD CONTENT
Topic
Content
Source link
Name
5+2 =