HTML5 - 限制文件上传

HTML5 - Restrict file upload

<input type="file" accept=".csv" /> 

以上代码还允许上传 .txt 或任何类型的文件。

如何限制html5中的其他文件类型

使用输入属性,您可以指定要上传的任何文件扩展名。要添加多个属性,请用逗号分隔它们:

<input type="file" accept=".txt,.jpg">

您还可以设置预定义的扩展系列以供上传。只需分别使用 audio/*video/*image/* 到 select 这些文件类型。这些也可以与常规文件扩展名结合使用。例如

<input type="file" accept="image/*,.psd">

这让您可以上传图像或 Photoshop 文件。

希望对您有所帮助!

试试这个

<input id="fileSelect" type="file" accept=".csv, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel" />

另请查看更多详细信息http://jsfiddle.net/dirtyd77/LzLcZ/144/

你不能在html5文件输入中限制其他文件类型,如果用户select其他文件它允许select。属性 'accept' 只是向用户提示支持的文件类型。