有限文件类型的HTML文件输入字段? [英] HTML file input field with limited file types?

查看:103
本文介绍了有限文件类型的HTML文件输入字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有HTML文件输入字段,有什么方法可以将其限制为仅允许图片上传?我相信这对于Flash或Java来说是可能的,但我现在试图远离他们。



我发现accept属性在线,但似乎没有为我做任何事情(或者我错用了它)。任何帮助或例子的赞赏。

解决方案

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

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

  if((document.form1.upload.value.lastIndexOf(。jpg )==  -  1){
alert(请只上传.jpg扩展文件);
返回false;
}

通过



如果您使用jquery,请检查以下问题:如何让jQuery限制上传文件类型?

您也可以使用JQuery验证插件。

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

via


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.

解决方案

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)

这篇关于有限文件类型的HTML文件输入字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆