繁体   English   中英

限制要在mvc中上传的文件数

[英]limit number of files to be uploaded in mvc

在我的MVC应用程序中,我有信息上传表格。 用户可以在这里上传视频和图像。 为此,我创建了文件控件,一个用于视频,一个用于图像。 在这里,用户只能为单个表单上传最多2个视频和最多5个图像。 谁能建议我如何限制用户上传文件的数量。 即如果只有2个视频和5个图像,我还应该在哪里实现最佳使用? controller或使用javascript吗?

MVC文件上传方法:

<input type="file" name="file" id="file" />

这会将HttpPostedFileBase传递给控制器​​,或者如果您有多个IEnumerable<HttpPostedFileBase> 然后,您可以评估文件和文件类型的数量,并将所有功能/消息返回给用户。

[HttpPost]
public ActionResult Index(HttpPostedFileBase file) {...

要么

<input type="file" name="files" id="file" />
<input type="file" name="files" id="file" />

[HttpPost]
public ActionResult Index(IEnumerable<HttpPostedFileBase> files) {...

我尝试使用javascript

文件上传控件:

<input type="file" id="t" name="t" onchange="checkFilesCount(this)" multiple>

JavaScript代码:

function checkFilesCount(id) {
    if(id.files.length > 5)
    {    
        alert('you cant upload files more than 5');
        document.getElementById("t").value = "";
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM