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

前端 未结 11 2189
盖世英雄少女心
盖世英雄少女心 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:55

    You can use the change event to monitor what the user selects and notify them at that point that the file is not acceptable. It does not limit the actual list of files displayed, but it is the closest you can do client-side, besides the poorly supported accept attribute.

    var file = document.getElementById('someId');
    
    file.onchange = function(e) {
      var ext = this.value.match(/\.([^\.]+)$/)[1];
      switch (ext) {
        case 'jpg':
        case 'bmp':
        case 'png':
        case 'tif':
          alert('Allowed');
          break;
        default:
          alert('Not allowed');
          this.value = '';
      }
    };
    <input type="file" id="someId" />

    JSFiddle

    0 讨论(0)
  • 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:

    <form method="post" onsubmit="return beforeSubmit();">
        <input type="file" id="ifile" name="ifile"/>
    </form>
    
    0 讨论(0)
  • 2020-11-22 04:59

    There is the accept attribute for the input tag. However, it is not reliable in any way. Browsers most likely treat it as a "suggestion", meaning the user will, depending on the file manager as well, have a pre-selection that only displays the desired types. They can still choose "all files" and upload any file they want.

    For example:

    <form>
        <input type="file" name="pic" id="pic" accept="image/gif, image/jpeg" />
    </form>

    Read more in the HTML5 spec

    Keep in mind that it is only to be used as a "help" for the user to find the right files. Every user can send any request he/she wants to your server. You always have to validated everything server-side.

    So the answer is: no you cannot restrict, but you can set a pre-selection but you cannot rely on it.

    Alternatively or additionally you can do something similar by checking the filename (value of the input field) with JavaScript, but this is nonsense because it provides no protection and also does not ease the selection for the user. It only potentially tricks a webmaster into thinking he/she is protected and opens a security hole. It can be a pain in the ass for users that have alternative file extensions (for example jpeg instead of jpg), uppercase, or no file extensions whatsoever (as is common on Linux systems).

    0 讨论(0)
  • 2020-11-22 05:08

    You could actually do it with javascript but remember js is client side, so you would actually be "warning users" what type of files they can upload, if you want to AVOID (restrict or limit as you said) certain type of files you MUST do it server side.

    Look at this basic tut if you would like to get started with server side validation. For the whole tutorial visit this page.

    Good luck!

    0 讨论(0)
  • 2020-11-22 05:09

    As mentioned in previous answers we cannot restrict user to select files for only given file formats. But it's really handy to use the accept tag on file attribute in html.

    As for validation, we have to do it at the server side. We can also do it at client side in js but its not a foolproof solution. We must validate at server side.

    For these requirements I really prefer struts2 Java web application development framework. With its built-in file upload feature, uploading files to struts2 based web apps is a piece of cake. Just mention the file formats that we would like to accept in our application and all the rest is taken care of by the core of framework itself. You can check it out at struts official site.

    0 讨论(0)
提交回复
热议问题