Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

html file input only select one file

Tags:

html

Hi I want that the input file accept only one file.

I mean when I try select file for upload only one file can be select

I know that we can use multiple like this

<input type="file" name="img" multiple>

but can we use something like this?

<input type="file" name="img" multiple = false>

this is my code

<form action="/Panel/Uploader" method="post" enctype="multipart/form-data" class="dropzone" id="dropzoneForm">
                        <div class="fallback">
                            <input name="file" type="file" accept="image/jpeg, image/png, image/gif" />
                            <input type="submit" value="Upload" />
                        </div>
                    </form>
like image 698
keyone2693 Avatar asked Feb 28 '19 15:02

keyone2693


People also ask

How do I restrict multiple file uploads in HTML?

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.


2 Answers

Here is the codepen link.

Don't use multiple attribute.

<input type="file" name="img">

We can see that we can't select more than one file.

Tested on Mozilla and chrome.

You can even restrict using js

$(function(){
    $("input[type='submit']").on('click', () => {
        var $fileUpload = $("input[type='file']");
        if (parseInt($fileUpload.get(0).files.length)>1){
         // handle here 
           return false;
        }
    });    
});
like image 58
inrsaurabh Avatar answered Oct 19 '22 01:10

inrsaurabh


You can't like this:

<input type="file" name="img" multiple = false>

But if you wan't only to select one file you can do something like this:

<input type="file" name="img">

You just have to remove "multiple".

like image 27
Mikev Avatar answered Oct 19 '22 01:10

Mikev