简体   繁体   English

asp.net mvc中的多文件上传

[英]Multiple File Upload in asp.net mvc

I need to get files from a single file uploader and a multiple file uploader from the same form. 我需要从同一个表单中获取单个文件上传器和多个文件上传器的文件。 And also need to know from which input field those files are coming. 并且还需要知道这些文件来自哪个输入字段。 From Request.Files i can get all files but can't know from which field those file are coming. 从Request.Files我可以获取所有文件,但无法知道这些文件来自哪个字段。

I have a form. 我有一张表格。

<form> 
    <input type="file" name="file1">
    <input type="file" name="files" multiple="true"> 
</form>`

Use a model instead of Request.Files directly. 直接使用模型而不是Request.Files Based off your view you could do something like this: 基于您的观点,您可以执行以下操作:

public class UploadForm
{
    public HttpPostedFileBase file1 {get;set;}

    public IEnumerable<HttpPostedFileBase> files {get;set;}
}

And then in your action: 然后在你的行动中:

public ActionResult Uploade(UploadForm form)
{
    if(form.file1 != null)
    {
        //handle file
    }

    foreach(var file in form.files)
    {
        if(file != null)
        {
            //handle file
        }
    }
    ...
}

If these two upload controls have different name attributes you can let the model binder do the work. 如果这两个上传控件具有不同的名称属性,则可以让模型绑定器完成工作。 You just have to name the parameter in the controller action the same as the name of your upload control. 您只需将控制器操作中的参数命名为与上载控件的名称相同。

public ActionResult Upload(HttpPostedFileBase file1, IEnumerable<HttpPostedFileBase> files)
{
    ...
}

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

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