Limit file format when using <input type=“file”>?

前端 未结 11 2198
盖世英雄少女心
盖世英雄少女心 2020-11-22 04:15

I\'d like to restrict the type of file that can be chosen from the native OS file chooser when the user clicks the Browse button in the

11条回答
  •  感情败类
    2020-11-22 04:57

    Yes, you are right. It's impossible with HTML. User will be able to pick whatever file he/she wants.

    You could write a piece of JavaScript code to avoid submitting a file based on its extension. But keep in mind that this by no means will prevent a malicious user to submit any file he/she really wants to.

    Something like:

    function beforeSubmit()
    {
        var fname = document.getElementById("ifile").value;
        // check if fname has the desired extension
        if (fname hasDesiredExtension) {
            return true;
        } else {
            return false;
        }
    }
    

    HTML code:

提交回复
热议问题