Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Input type=File "Accept" property does not work in Chrome?

Have been through quite some documentation but don't seem to get this very basic thing working in Chrome.

<input type="file" accept=".jpg, .png"/>

The dialog that opens just shows "Custom Files" in the extension drop-down. See this Fiddle.

enter image description here

It only seems to work with a single extension specification. Also tried using some mime types to no avail.

like image 538
Bernoulli IT Avatar asked Mar 07 '18 14:03

Bernoulli IT


1 Answers

I am agree with @Eugenio comment. You can validate it at client-side and server-side.

if you use accept="image/*" instead Custom Files it will show Image Files

enter image description here

 function validate() {
  var fileName = document.getElementById("fileType").value;
  var dot = fileName.lastIndexOf(".") + 1;
  var extFile = fileName.substr(dot, fileName.length).toLowerCase();
  if (extFile == "jpg" || extFile == "jpeg") {
   //accepted
  } else {
   alert("Only jpg and jpeg image files allowed!");
  }
 }
<input type="file" id="fileType" accept="image/*" onchange="validate()"/>
like image 118
Rahul Mahadik Avatar answered Oct 17 '22 23:10

Rahul Mahadik