使用多文件输入时如何限制选择的最大文件数 [英] How to limit the maximum files chosen when using multiple file input

查看:33
本文介绍了使用多文件输入时如何限制选择的最大文件数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用 时,用户可以选择多个文件.

When using <input type="file" multiple> it's possible for the user to choose multiple files.

如何设置可以选择的文件数量的限制,例如两个?

How does ones sets a limit for how many files can be chosen, for instance two?

推荐答案

你可以运行一些 jQuery 客户端验证来检查:

You could run some jQuery client-side validation to check:

$(function(){
    $("input[type='submit']").click(function(){
        var $fileUpload = $("input[type='file']");
        if (parseInt($fileUpload.get(0).files.length)>2){
         alert("You can only upload a maximum of 2 files");
        }
    });    
});​

http://jsfiddle.net/Curt/u4NuH/

但请记住也要检查服务器端,因为客户端验证可以很容易地绕过.

But remember to check on the server side too as client-side validation can be bypassed quite easily.

这篇关于使用多文件输入时如何限制选择的最大文件数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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