手机端上传文件限制类型

MIME类型

  <body>
    <input type="file" name="" id="f" />
    <script>
      const qs = v => document.querySelector(v);
      const types = [
        'video/mp4',
        'video/ogg',
        'image/jpeg',
        'image/jpg',
        'image/png',
        'image/webp',
      ];
      const fEl = qs('#f');

      fEl.addEventListener('change', e => {
        const files = fEl.files;
        if (!files.length) return;

        const file = files[0];
        const fType = file.type;

        if (types.some(typeStr => typeStr === fType)) {
          console.log(file);
        } else {
          fEl.value = '';
        }
      });
    </script>
  </body>
posted @ 2019-01-28 14:50  Ajanuw  阅读(598)  评论(0编辑  收藏  举报