Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap File Input - limit file types

Code that has been developed for a company that I am coaching uses Krajee's Bootstrap File Input. Where this function is called, allowedFileExtensions is set to allow only jpg, gif, and png. However, when the browse button is clicked, the list if allowed image file types shows about one dozen images types. If one that is not of the three types is selected, e.g., svg, the system shows an error message indicating that the file type is not allowed.

While this certainly works, the browse should only allow the chosen types to be selected. What additional parameter(s) and/or other changes are needed so that the browse window limits the selection to the chosen types?

If it matters: I have seen this behavior on a PC with both Firefox and IE.

like image 946
GRoston Avatar asked Oct 20 '15 21:10

GRoston


People also ask

How can restrict users upload max 10 files at once?

Complete HTML/CSS Course 2022 To allow multiple file uploads in HTML forms, use the multiple attributes. The multiple attributes work with email and file input types. For limiting maximum items on multiple inputs, use JavaScript. Through this, limit the number of files to be uploaded.

How do you specify a file type in input?

The <input type="file"> defines a file-select field and a "Browse" button for file uploads. To define a file-select field that allows multiple files to be selected, add the multiple attribute. Tip: Always add the <label> tag for best accessibility practices!

How do I restrict a file type in HTML?

Solution with HTML attributes But it is possible to restrict the file types to only images, or certain image file extensions. To achieve this, you need to use the HTML accept attribute. This attribute is only used with <input type="file"> and serves as a filter to select file inputs from the file input dialog box.

How do I upload a file using bootstrap?

To create a custom file upload, wrap a container element with a class of . custom-file around the input with type="file". Then add the . custom-file-input to it.


2 Answers

While this certainly works, the browse should only allow the chosen types to be selected. What additional parameter(s) and/or other changes are needed so that the browse window limits the selection to the chosen types?

Try setting accepted types of file extension for input type="file" element using accept attribute , with value being file extensions separated by comma . Only file extnsion types defined as value of accept attribute should be displayed at "Open File" dialog

<input type="file" accept=".jpg,.gif,.png" />
like image 136
guest271314 Avatar answered Sep 21 '22 03:09

guest271314


Here is what I used.

//update file name text box with selected filepath and file
$('#FileUpload1').change(function ()
{
    var filename = $(this).val();

    $('#txtFileName').val(filename);

    var fileExtension = ['xlsx', 'xls', 'csv'];
    if ($.inArray(filename.split('.').pop().toLowerCase(), fileExtension) == -1)
    {
        $('#lblWarning').show();
        $('#lblWarning').text("Only 'xlsx', 'xls', 'csv' formats are allowed.");
    }
    else
    {
        $('#lblWarning').hide();
    }


});
like image 45
Joseph Norris Avatar answered Sep 21 '22 03:09

Joseph Norris