html - File input 'accept' attribute - is it useful? -
implementing file upload under html simple, noticed there 'accept' attribute can added <input type="file" ...>
tag.
is attribute useful way of limiting file uploads images, etc? best way use it?
alternatively, there way limit file types, preferably in file dialog, html file input tag?
the accept
attribute incredibly useful. hint browsers show files allowed current input
. while can typically overridden users, helps narrow down results users default, can they're looking without having sift through hundred different file types.
usage
note: these examples written based on current specification , may not work in (or any) browsers. specification may change in future, break these examples.
h1 { font-size: 1em; margin:1em 0; } h1 ~ h1 { border-top: 1px solid #ccc; padding-top: 1em; }
<h1>match image files (image/*)</h1> <p><label>image/* <input type="file" accept="image/*"></label></p> <h1>match video files (video/*)</h1> <p><label>video/* <input type="file" accept="video/*"></label></p> <h1>match audio files (audio/*)</h1> <p><label>audio/* <input type="file" accept="audio/*"></label></p> <h1>match image files (image/*) , files extension ".someext"</h1> <p><label>.someext,image/* <input type="file" accept=".someext,image/*"></label></p> <h1>match image files (image/*) , video files (video/*)</h1> <p><label>image/*,video/* <input type="file" accept="image/*,video/*"></label></p>
from html specification (source)
the
accept
attribute may specified provide user agents hint of file types accepted.if specified, attribute must consist of set of comma-separated tokens, each of must ascii case-insensitive match 1 of following:
the string
audio/*
- indicates sound files accepted.
the string
video/*
- indicates video files accepted.
the string
image/*
- indicates image files accepted.
a valid mime type no parameters
- indicates files of specified type accepted.
a string first character u+002e full stop character (.)
- indicates files specified file extension accepted.
Comments
Post a Comment