How to check file MIME type with javascript before upload?

前端 未结 9 2111
挽巷
挽巷 2020-11-22 03:50

I have read this and this questions which seems to suggest that the file MIME type could be checked using javascript on client side. Now, I understand that the real validati

相关标签:
9条回答
  • 2020-11-22 04:45

    This is what you have to do

    var fileVariable =document.getElementsById('fileId').files[0];
    

    If you want to check for image file types then

    if(fileVariable.type.match('image.*'))
    {
     alert('its an image');
    }
    
    0 讨论(0)
  • 2020-11-22 04:51

    Here is an extension of Roberto14's answer that does the following:

    THIS WILL ONLY ALLOW IMAGES

    Checks if FileReader is available and falls back to extension checking if it is not available.

    Gives an error alert if not an image

    If it is an image it loads a preview

    ** You should still do server side validation, this is more a convenience for the end user than anything else. But it is handy!

    <form id="myform">
        <input type="file" id="myimage" onchange="readURL(this)" />
        <img id="preview" src="#" alt="Image Preview" />
    </form>
    
    <script>
    function readURL(input) {
        if (window.FileReader && window.Blob) {
            if (input.files && input.files[0]) {
                var reader = new FileReader();
                reader.onload = function (e) {
                    var img = new Image();
                    img.onload = function() {
                        var preview = document.getElementById('preview');
                        preview.src = e.target.result;
                        };
                    img.onerror = function() { 
                        alert('error');
                        input.value = '';
                        };
                    img.src = e.target.result;
                    }
                reader.readAsDataURL(input.files[0]);
                }
            }
        else {
            var ext = input.value.split('.');
            ext = ext[ext.length-1].toLowerCase();      
            var arrayExtensions = ['jpg' , 'jpeg', 'png', 'bmp', 'gif'];
            if (arrayExtensions.lastIndexOf(ext) == -1) {
                alert('error');
                input.value = '';
                }
            else {
                var preview = document.getElementById('preview');
                preview.setAttribute('alt', 'Browser does not support preview.');
                }
            }
        }
    </script>
    
    0 讨论(0)
  • 2020-11-22 04:53

    If you just want to check if the file uploaded is an image you can just try to load it into <img> tag an check for any error callback.

    Example:

    var input = document.getElementsByTagName('input')[0];
    var reader = new FileReader();
    
    reader.onload = function (e) {
        imageExists(e.target.result, function(exists){
            if (exists) {
    
                // Do something with the image file.. 
    
            } else {
    
                // different file format
    
            }
        });
    };
    
    reader.readAsDataURL(input.files[0]);
    
    
    function imageExists(url, callback) {
        var img = new Image();
        img.onload = function() { callback(true); };
        img.onerror = function() { callback(false); };
        img.src = url;
    }
    
    0 讨论(0)
提交回复
热议问题