1

I am using krajee-file-input-plugin.I want to clear selected file based of file type.I have tried the following code but no result.

 @Html.TextBoxFor(m => m.PhotoUrl,
                class = "form-control imgUpload", @placeholder = "Please upload Photo",
                @id = "txtPhoto", @type = "file" })

 //fileUpload plugin
$(".imgUpload").fileinput({
    showUpload: false
});


 $(".imgUpload").change(function () {
    var ext = $(this).val().split('.').pop().toLowerCase();
    switch (ext) {
        case 'jpg':
        case 'jpeg':
        case 'png':
        case 'gif':                
            break;
        default:
            $(this).remove();
            alert('This is not an allowed file type.');

    }
});

Here $(this).remove() function is not working properly

1 Answer 1

1

Solved the above scenario by the below piece of code.

Hope it may help some one :)

$('.imgUpload').on('fileloaded', function (event, previewId) {
    var id = $(this).attr("id");
    var ext = $(this).val().split('.').pop().toLowerCase();
        if (ext == 'jpg' || ext == 'jpeg' || ext == 'png' || ext == 'gif') {
            return true
        }
        else {
            bootbox.alert("You can upload only files of type <strong>jpg ,jpeg ,png ,gif </strong>", function () {
                $("#"+id).fileinput('clear');
            });

        }
});
Sign up to request clarification or add additional context in comments.

Comments

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.