HTML <input type='file'> apply a filter

前端 未结 8 1871
清酒与你
清酒与你 2020-11-28 12:01

now when i will click the browse button, the browse dialog will show all files... what if i want to f

8条回答
  •  余生分开走
    2020-11-28 12:24

    You can't control which files can be selected, but you can read the filename with javascript after the file is chosen. You could then display a warning and/or disable the submit button if the file is the wrong type. However, remember that you can't rely on the extension to tell you whether or not the file is really of the right type. It should only be treated as a way to help users who might otherwise waste time uploading a huge file before discovering that you don't support that file type.

    Here's some example code to do that. It uses jQuery, but you could do the same in plain javascript too.

    $(function() {
        $('#inputId').change( function() {
            var filename = $(this).val();
            if ( ! /\.txt$/.test(filename)) {
                alert('Please select a text file');
            }
        });
    });
    

提交回复
热议问题