Display only XML files in an input element of an HTML file

How can I show only XML files in a file input element? I read about the accept attribute, but also found out that the browser does not support it. I found several JS scripts here, but they did not work: - \

(I also check this server side, but I would like to do this on the client side as well)

thanks

+8
javascript html input forms selection
source share
2 answers

The answer is pretty simple, you are using the MIME type. So, if this is the xml file you want, you simply do:

<input type="file" accept="text/xml" /> 

If you want to see a complete list of MIME types, check out this site http://www.iana.org/assignments/media-types/media-types.xhtml

+13
source share

Not sure if you can do this, but at least you can use the callback function and check the input value for the .xml extension.

Excerpt:

 <script type="text/javascript"> function isXml(input) { var value = input.value; var res = value.substr(value.lastIndexOf('.')) == '.xml'; if (!res) { input.value = ""; } return res; } </script> <form method="post" action=""> <input type="file" name="myfile" id="myfile" onchange="return isXml(this)" /> <input type="submit" /> </form> 
-one
source share

All Articles