input type=file 的 maxLength是多少

2016-03-28 11:27:57input, type-file, maxlength问题
var input = document.querySelector("[type=file]");
input.addEventListener('change', function (e) {
    var maxLength = this.maxLength;
    console.log(maxLength);//524288
}, false);

这里的maxLength是浏览器支持的type=file的最大值吗

网友回答:

  • 谷易 谷易 2016-3-28 2:11 回复:
    已被采纳

maxlength和minlength支持的type类型为:text,tel,url,email,password,以及search。当type=file时,maxlength是没有效果的,标准如下:
clipboard.png


  • 外籍杰克 外籍杰克 2016-3-28 9:58 回复:

    以下这句话上面也都说到了,就是maxLength对哪些类型的input有效

If the value of the type attribute is text, email, search, password, tel, or url, this attribute specifies the maximum number of characters (in Unicode code points) that the user can enter; for other control types, it is ignored.

这里解释下524288。这个值是maxLength的默认值,就算你对一个空的input(<input>)进行输出maxLength,也是524288。

w3c说maxLength的默认值是unlimited,但实际上至少在webkit的浏览器上默认值为512KB(524288)。可能因为某种保护措施,但也有人认为这个是webkit的bug。

google以下,啥都有

  • 外籍杰克 外籍杰克 2016-3-29 9:17 回复:外籍杰克

    @jsoncode 要不,赞一个咯( ' – ' )

  • jsoncode jsoncode 2016-3-29 9:09 回复:外籍杰克
    真心感谢



  • 虛無縹緲 虛無縹緲 2016-3-28 3:04 回复:

    type=file是用于选取文件的,不具有maxLength这属性,input的type为text,url,email,password等文字编辑型的才具有maxLength属性,表示最大可输入长度,超出此长度就无法输入了

    • jsoncode jsoncode 2016-3-28 5:44 回复:虛無縹緲
      那我打印出来的这个数值是什么?我很想知道

  • yuweilan yuweilan 2016-4-6 6:10 回复:

    借地儿问下,file如果上传的是图片,怎么限制选中上传的图片数量?

    • jsoncode jsoncode 2016-4-6 6:51 回复:yuweilan
      this.files是一个数组,你可以对它进行限制

    • LazyHua LazyHua 2018-3-1 2:08 回复:yuweilan

      解决了吗这个