简体   繁体   English

如何允许<input type="“file”">只接受.jpeg 和.jpg 文件?

[英]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:我的系统以正确的显示格式打开文件,如下所示:

在ubuntu中打开文件

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:它不再向用户显示他们应该使用哪种格式,如您所见:

在ubuntu中打开文件

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.

相关问题 如何在Safari中允许输入类型=文件仅接受图像文件 - How to allow input type=file to accept only images files in Safari 如何允许<input type="file">只接受图像文件? - How to allow <input type="file"> to accept only image files? 只允许.Jpg和.Jpeg - Allow only .Jpg And .Jpeg <input type=“file” name=“img” accept=“.jpg”>仅检索完整路径插入的图像名称,如何解决? - <input type=“file” name=“img” accept=“.jpg”> is retriving only image name insted of full path, how to fix it? 如何强制输入类型文件仅接受.html或.htm文件 - how to force input type file to accept only .html or .htm files Firefox有时会忽略文件输入中带有.jpg和.jpeg扩展名的JPEG文件 - Firefox sometimes ignores JPEG files with .jpg and .jpeg extensions on file input 仅允许使用Jpg和Jpeg文件 - Only Jpg and Jpeg files are allowed 如何让 HTML 的“文件”类型的输入元素只接受 pdf 个文件? - How to get the HTML's input element of "file" type to only accept pdf files? 如何限制我的输入类型=“文件”只接受在Firefox中不起作用的png图像文件 - How to restrict my input type=“file” to accept only png image files not working in Firefox 如何使输入类型只接受PDF文件? - How to make input type should only accept PDF files?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM