具有有限文件类型的HTML文件输入字段?(HTML file input field with limited file types?)

编程入门 行业动态 更新时间:2024-10-08 18:34:38
具有有限文件类型的HTML文件输入字段?(HTML file input field with limited file types?)

如果我有一个HTML文件输入字段,有什么办法可以限制它只允许图像上传? 我确信这可以用Flash或Java实现,但我暂时试图远离它们。

我在网上找到了“接受”属性,但它似乎没有为我做任何事情(或者我使用它错了。)。 任何帮助或示例表示赞赏。

If I have an HTML file input field, is there any way that I can limit it to only allow image uploads? I'm sure this would be possible with Flash or Java, but I'm trying to stay away from them for the time being.

I found the "accept" attribute online, but it doesn't seem to be doing anything for me (or I'm using it wrong.). Any help or examples is appreciated.

最满意答案

最好的方法是在后端处理它。 根据您要允许的类型类型和后端技术,您可以找到各种方法。

您也可以在javascript中执行此操作,但是您将受到更多限制,并且可能会导致问题crossbrowser:

if((document.form1.upload.value.lastIndexOf(".jpg")==-1) { alert("Please upload only .jpg extention file"); return false; }

( 通过 )

如果你使用jquery,请查看这个问题: 如何让jQuery在上传时限制文件类型?

您还可以使用JQuery Validation插件。

$("#myform").validate({ rules: { field: { required: true, accept: "xls|csv" } } });

( 通过 )

The best way is to handle that in the backend. Depending on the type of types you want to allow and the backend technology you will find various ways of doing this.

You can also do this in javascript but you'll be more limited and it might lead to issues crossbrowser:

if((document.form1.upload.value.lastIndexOf(".jpg")==-1) { alert("Please upload only .jpg extention file"); return false; }

(via)

If you use jquery, checkout this question: How to have jQuery restrict file types on upload?

You can also use the JQuery Validation plugin.

$("#myform").validate({ rules: { field: { required: true, accept: "xls|csv" } } });

(via)

更多推荐

I'm,Flash,Java,HTML,电脑培训,计算机培训,IT培训"/> <meta name="desc

本文发布于:2023-07-27 08:30:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1287757.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字段   文件类型   文件   HTML   file

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!