-1

We are working on a Email Encrypted Service. Here I have designed a html page (User Registration) where I have provided an area for uploading a file with extentions .crt, .cer and .der

This is HTML Content:

<section>
    <label class="label">PubLic Key File</label>
<div> 
    <input type="file" id="fileInput">
</div>
    <div id="fileDisplayArea"></div>
</section>
<button type="submit" class="button">Submit</button>

Javascript Code is:

window.onload = function() {

    var fileInput = document.getElementById('fileInput');
    var fileDisplayArea = document.getElementById('fileDisplayArea');


    fileInput.addEventListener('change', function(e) {
        var file = fileInput.files[0];
        var imageType = /image.*/

        if (file.type.match(imageType)) {
            var reader = new FileReader();

            reader.onload = function(e) {
                fileDisplayArea.innerHTML = "";

                var img = new Image();
                img.src = reader.result;

                fileDisplayArea.appendChild(img);
            }

            reader.readAsDataURL(file); 
        } else {
            fileDisplayArea.innerHTML = "File not supported!";
        }
    });

}

I have copied this Javascript Code (beginner in javascript) . It Only Accepts image file. I want to change this code which only accepts .crt, .cer and .der Extentions.

Thank you :)

cstack
  • 2,152
  • 6
  • 28
  • 47

1 Answers1

0

Your current regex will actually match any filename that contains the word "image" (any part of the filename that is "image" followed by zero or more characters)

If you want to match filenames that end in ".crt", ".cer" or ".der", you can use this regex:

var imageType = /\.crt|cer|der$/

You can test regular expressions using Rubular

cstack
  • 2,152
  • 6
  • 28
  • 47