[英]How to allow <input type=“file”> to accept only .jpeg and .jpg files?
In my Angular 11 project, I have an image uploader component which uses <input type="file">
to accept an image and send it to server.在我的 Angular 11 项目中,我有一个图像上传器组件,它使用
<input type="file">
接受图像并将其发送到服务器。 This image uploader is supposed to only accept jpg and jpeg formats.这个图片上传器应该只接受 jpg 和 jpeg 格式。
When I add the accept
attribute to my input like so:当我像这样将
accept
属性添加到我的输入时:
<input
type="file"
accept="image/png"
/>
My system opens files with the correct showing format, like this:我的系统以正确的显示格式打开文件,如下所示:
Which is correct.哪个是对的。 It's showing the user that it only accepts.png image.
它向用户显示它只接受.png 图像。
But when I change the accept
attribute in my input to this:但是当我将输入中的
accept
属性更改为:
<input
type="file"
accept="image/jpg, image/jpeg"
/>
It no longer shows user which format they should be using, as you can see:它不再向用户显示他们应该使用哪种格式,如您所见:
It just says custom files on my ubuntu and I'm guessing all files in windows.它只是说我的 ubuntu 上的自定义文件,我猜 windows 中的所有文件。 Which is not what I want.
这不是我想要的。 The user should be able to see that they're only allowed to add jpg or jpeg files here.
用户应该能够看到他们只允许在此处添加 jpg 或 jpeg 文件。 something like *.jpg or *jpeg.
像 *.jpg 或 *jpeg 之类的东西。
Is there a way to fix this?有没有办法来解决这个问题?
try changing it to either accept="image/jpeg" or accept=".jpg, .jpeg"尝试将其更改为 accept="image/jpeg" 或 accept=".jpg, .jpeg"
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/file https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/file
Change it to something like将其更改为类似
<input type="file" accept=".jpg,.jpeg" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.