问题
so I have a standard <input type="file" accept="..."/>
where the accept, is a large list of different mime types. I already verify the filetypes on the server backend but I want to be sure I am not filtering out valid files on the clients UI. I realise older browsers will not filter files at all, this is fine. My concern is that a modern browser may not recognise a mime-type and filter out files that are actually valid.
So my question is basically, how reliable is the 'accept' attribute? For example will a browser recognise the .docx mime type if microsoft office is not installed? Also, some file types have many different possible mime-types associated with them, does it matter if I only list one of the mime-types?
I don't understand why the specification originally used mime-types, it would be significantly simpler and I am guessing more reliable if they have had just used extensions in the first place (like chrome accepts).
For the complete list of mimes I am currently using:
application/msword,
application/vnd.openxmlformats-officedocument.wordprocessingml.document,
application/rtf,
application/msexcel,
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,
application/vnd.openxmlformats-officedocument.spreadsheetml.template,
application/vnd.ms-excel.template.macroEnabled.12,
application/vnd.ms-excel.sheet.macroEnabled.12,
text/csv,
application/mspowerpoint,
application/vnd.openxmlformats-officedocument.presentationml.presentation,
application/vnd.ms-powerpoint.presentation.macroEnabled.12,
application/vnd.ms-powerpoint,
application/vnd.openxmlformats-officedocument.presentationml.template,
application/vnd.openxmlformats-officedocument.presentationml.slideshow,
application/pdf,
image/jpg,
image/jpeg,
image/png,
image/bmp,
image/gif,
text/plain,
message/rfc822,
application/vnd.oasis.opendocument.text,
application/vnd.oasis.opendocument.spreadsheet,
application/vnd.oasis.opendocument.presentation,
application/visio,application/vnd.visio,
application/vst,
application/vnd.visio.webdrawing,
application/x-mspublisher
回答1:
The specification allows you to use extensions as well as mime types
In fact, it actually recommends that you use both mime-type and extension.
As for requiring the OS to recognize the mime-type, that doesn't really make sense since mime-types are Internet protocol file identifiers. It actually depends on the user agent (browser) support for the mime-type. Since this is a fairly new construct, I wouldn't be surprised if browsers only minimally "support" it by having a common mapping between mime types and extensions. I can't imagine that the browser attempts to stat all readable files to determine mime type (which in my experience is not even close to dependable anyway). accept
's intent seems to be more about changing how the file upload dialog looks, and I doubt any browser does that yet, although I could definitely be wrong considering how quick browser releases are nowadays.
If you're still concerned, you could test yourself in common browsers since it really seems like it should be per user agent rather than per OS.
来源:https://stackoverflow.com/questions/14994320/do-the-values-in-the-accept-attribute-require-a-computer-to-know-the-mime-types