接受不在react中的文件类型中工作
我正在创建一个文件上传组件,其中我必须仅允许XLS
或xlsx
file.i am在下面写作代码。
<div>
<form onSubmit={handleSubmit}>
<input type="file" accept="xlsx/*" required onChange={handleChange}/>
<button type="submit" className='submitbutton'>Import Bulk User</button>
</form>
</div>
但这并不限制其他文件类型。
我如何限制其他文件类型,仅允许XLSX
I am creating a file upload component in which I have to allow only xls
or xlsx
file.I am writing below code.
<div>
<form onSubmit={handleSubmit}>
<input type="file" accept="xlsx/*" required onChange={handleChange}/>
<button type="submit" className='submitbutton'>Import Bulk User</button>
</form>
</div>
but it is not restricting other file types.
how can I restrict other file types and allow only xlsx
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
接受属性作为其价值comma-- comma---一种或多种文件类型或唯一文件类型指定符的分离列表,描述了要允许的文件类型:
The accept attribute takes as its value a comma-separated list of one or more file types, or unique file type specifiers, describing which file types to allow:
您仍然可以使用处理程序功能来验证文件扩展名。
获取文件扩展名
,然后检查其是否支持
演示
You could still use your handler function to validate the file extension.
Get the file extension by
and then check if its supported or not
DEMO
您应该更新输入的
accept
属性如下:更多有关接受: https://developer.mozilla.org/en-us/docs/web/html/attributes/accept
You should update your input's
accept
attribute as below:More about accept: https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/accept