0

I am working on Codeigniter, i am facing one problem i have to show the user error message when he is uploading any other file type to server using ajax. I do not want to load view again to show the error message. My code is as follows: Please help me to solve my problem.'

View:

function upload_video_Data(a)
{ 

        var fd = new FormData(document.getElementById('posting_comment_'+a));
        fd.append("file_m_id",a);
        var bar = $('.bar');
        var xhr = new XMLHttpRequest();        
        xhr.upload.addEventListener("progress", uploadProgress, false);

     xhr.onreadystatechange=function() {
       if (xhr.readyState==4 && xhr.status==200) {
        document.getElementById("nameTest").value=xhr.responseText;
        }
      }   
        xhr.open("POST", "Dashboard/do_upload_video");
        xhr.send(fd);

        function uploadProgress(evt) {
            if (evt.lengthComputable) {
            var percentComplete = Math.round(evt.loaded * 100 / evt.total);
            document.getElementById('progressNumber').innerHTML = percentComplete.toString() + '%';
            $("#status").animate( { width: percentComplete.toString()+"%"}, 5);
            }
        }
}

Controller:

public function do_upload_video()
    {  
      $lecture_id=$_POST['file_m_id'];
      $output_dir = "./uploads/";
      $fileName = $_FILES["save_movie_".$lecture_id]["name"];

if(!move_uploaded_file($_FILES["save_movie_".$lecture_id]["tmp_name"],$output_dir.$fileName))
{
echo '0';
}
else
{
echo '1';
}
    }

1 Answer 1

1

Use this code before your ajax to validate the extension of file

var ext = $('#my_file_id').val().split('.').pop().toLowerCase();
    if($.inArray(ext, ['gif','png','jpg','jpeg']) == -1) {
        alert('invalid extension!');
    }

Now your code look like this

function upload_video_Data(a)
{ 

   var ext = $('#my_file_id').val().split('.').pop().toLowerCase();
if($.inArray(ext, ['gif','png','jpg','jpeg']) == -1) {
    return false;
}


        var fd = new FormData(document.getElementById('posting_comment_'+a));
        fd.append("file_m_id",a);
        var bar = $('.bar');
        var xhr = new XMLHttpRequest();        
        xhr.upload.addEventListener("progress", uploadProgress, false);

     xhr.onreadystatechange=function() {
       if (xhr.readyState==4 && xhr.status==200) {
        document.getElementById("nameTest").value=xhr.responseText;
        }
      }   
        xhr.open("POST", "Dashboard/do_upload_video");
        xhr.send(fd);

        function uploadProgress(evt) {
            if (evt.lengthComputable) {
            var percentComplete = Math.round(evt.loaded * 100 / evt.total);
            document.getElementById('progressNumber').innerHTML = percentComplete.toString() + '%';
            $("#status").animate( { width: percentComplete.toString()+"%"}, 5);
            }
        }
}
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks Ricky for ur answer. I want to clear one more thing in the code above i have used XMLHttpRequest where in this code we will return the response from the controller. I am using this code but it is not working xhr.onreadystatechange=function() { if (xhr.readyState==4 && xhr.status==200) { document.getElementById("nameTest").value=xhr.responseText; } }
i have updated my code, i want to return these 0 and 1 to the xmlhttprequest function, how we can do it.
write this code inside your function upload_video_Data(a) and update the ID of your input file if the validation fail add return false in place of alert('invalid extension!');
will i get 1 or 0 response from controller in xhr.responseText;
i am getting blank value in ext variable.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.