Home > database >  How to restrict some files while uploading a folder using HTML file input?
How to restrict some files while uploading a folder using HTML file input?

Time:07-07

While uploading a folder using HTML file input tag, I want to upload only all text files that are in that folder(Suppose folder is having some text files, database files, config files etc.). How can I do this?

CodePudding user response:

You can specify the accept attribute of the input file.

<input type="file" accept=".txt" />

For more resources.

https://www.w3schools.com/tags/att_input_accept.asp#:~:text=The accept attribute specifies a,be validated on the server.

CodePudding user response:

To make only text files show up when you use the input use the accept attribute like this:

<input type="file" accept=".doc,.docx,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document" />

But to be sure you should validate the value from the input in Javascript as well to add an extra layer of security.

CodePudding user response:

You can add accept attribute to the <input type="file">.

This Attibute can only be used with <input type="file">

Attribute specifies the filter for what file types the user can select from the file input dialog box.

<input type="file" accept=".txt"/>

There are other values to this attributes :

audio/* - pick all sound files

image/* - pick all image files

video/* - pick all video files

You can specify the file extension like .gif, .png, .doc etc.

As for your expectation - It is impossible to filter out only .txt files from the dialog box. Browser just make sure if the user has picked appropriate file as per written in accept attribute.

CodePudding user response:

It would be to validate only .txt files on upload so that it only recognizes these with the attribute in the accept=".txt" tag On this page you can find more information: https://www.htmlquick.com/es/reference/tags/input-file.html

  • Related