File input 'accept' attribute - is it useful?

前端 未结 8 1779
独厮守ぢ
独厮守ぢ 2020-11-22 02:30

Implementing a file upload under html is fairly simple, but I just noticed that there is an \'accept\' attribute that can be added to the

8条回答
  •  感动是毒
    2020-11-22 03:03

    The accept attribute is incredibly useful. It is a hint to browsers to only show files that are allowed for the current input. While it can typically be overridden by users, it helps narrow down the results for users by default, so they can get exactly what they're looking for without having to sift through a hundred different file types.

    Usage

    Note: These examples were written based on the current specification and may not actually work in all (or any) browsers. The specification may also change in the future, which could break these examples.

    h1 { font-size: 1em; margin:1em 0; }
    h1 ~ h1 { border-top: 1px solid #ccc; padding-top: 1em; }

    Match all image files (image/*)

    Match all video files (video/*)

    Match all audio files (audio/*)

    Match all image files (image/*) and files with the extension ".someext"

    Match all image files (image/*) and video files (video/*)

    From the HTML Specification (source)

    The accept attribute may be specified to provide user agents with a hint of what file types will be accepted.

    If specified, the attribute must consist of a set of comma-separated tokens, each of which must be an ASCII case-insensitive match for one of the following:

    The string audio/*

    • Indicates that sound files are accepted.

    The string video/*

    • Indicates that video files are accepted.

    The string image/*

    • Indicates that image files are accepted.

    A valid MIME type with no parameters

    • Indicates that files of the specified type are accepted.

    A string whose first character is a U+002E FULL STOP character (.)

    • Indicates that files with the specified file extension are accepted.

提交回复
热议问题