Input limiting file type

Asked

Viewed 2,502 times

3

My form has an input file field, and would like to limit the file type to select only images and pdf.

<form>
<input type='file' required>
<input type='submit' name='Enviar'>
</form>

I’m treating the file type in the back-end, but would like to limit the file type to be selected in the front-end.

2 answers

5


Just use the accept:

<form action="/action_page.php">
  <input type="file" name="pic" accept="image/*">
  <input type="submit">
</form> 

To determine the type, you need to know a little about Mime-Types. In the example above, any image serves.

Could have used image/jpeg, for example, if you wanted to restrict more.

As answered by @DVD, in your case the syntax is this:

<input type='file' required accept="image/*, application/pdf">

Separating desired types by comma.

Basic examples:

text/plain
text/html
image/jpeg
image/png
audio/mpeg
audio/ogg
audio/*
video/mp4
application/octet-stream

Learn more about Mime-Types here:

https://developer.mozilla.org/en-US/docs/Web/HTTP/Basico_sobre_HTTP/MIME_types

  • Round, thanks for the help!

  • @DVD I think valid restore your.

0

To accept images and PDF, you can include in the attribute accept the two mime-types separated by comma:

accept="image/*, application/pdf"

So your input would look like this:

<input type='file' required accept="image/*, application/pdf">

Testing:

<form>
<input type='file' required accept="image/*, application/pdf">
<input type='submit' name='Enviar'>
</form>

  • Do you want me to remove my answer and correct yours? I can do this as a sign of consideration.

  • No need not. It was just an observation to make clear that I notice this, already removed. Inclusive, yours is more complete, so I will not create case. By the way, if you’re negative, make sure it’s not me (seriously, I get P of life with dirty, but negative in good things is not me)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.