简体   繁体   English

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

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

In my MVC application, i have information upload form. 在我的MVC应用程序中,我有信息上传表格。 Here user can upload videos and images. 用户可以在这里上传视频和图像。 To do this i have created to file controls, one for videos and one for images. 为此,我创建了文件控件,一个用于视频,一个用于图像。 Here user should be only able to upload at max 2 videos and at max 5 images for single form. 在这里,用户只能为单个表单上传最多2个视频和最多5个图像。 Can any one suggest me how can i limit the user to upload number of files. 谁能建议我如何限制用户上传文件的数量。 ie if only 2 videos and 5 images also where should i implement this for best use? 即如果只有2个视频和5个图像,我还应该在哪里实现最佳使用? in controller or using javascript ? controller或使用javascript吗?

The MVC file upload approach: MVC文件上传方法:

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

This passes a HttpPostedFileBase to the controller or if you have multiple IEnumerable<HttpPostedFileBase> . 这会将HttpPostedFileBase传递给控制器​​,或者如果您有多个IEnumerable<HttpPostedFileBase> You can then evaluate the number of files and file types and return any functionality/messages back to the user. 然后,您可以评估文件和文件类型的数量,并将所有功能/消息返回给用户。

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

Or 要么

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

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

I tried using javascript 我尝试使用javascript

File upload control : 文件上传控件:

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

javascript code : 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