MVC从不同的文件输入上传文件 [英] MVC Upload files from different file inputs

查看:54
本文介绍了MVC从不同的文件输入上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在同一视图上有2个不同的文件输入.我想从文件输入A传递多个文件,从文件输入B传递一个文件.到目前为止,我的代码将允许我将A或B的内容传递给我的控制器,但不能同时传递两者.关于如何进行这项工作的想法?请注意,我正在使用MVC,并且页面上还有其他输入,而不仅仅是文件输入.

I have 2 different file inputs on the same view. I want to pass multiple files from file input A, and one from file input B. As of now, my code will allow me to pass the contents of A or B, but not both, to my controller. Thoughts on how to make this work? Please note that I am using MVC, and there are other inputs on the page, not just the file inputs.

有些不包含的javascript会将第一个输入的文本更改为上载的单个文件的名称,或者说选择了3个文件".javascript中没什么特别的.

There is some javascript that's not included that changes the text for the first input either to the name of the single file uploaded or it'll say '3 files selected'. Nothing special going on in javascript.

这正是我现在想做的,而我现在不能做.我有2种不同的文件输入,用户可以上传到.我需要两个文件输入都传递回控制器.不只是一个文件输入.文件输入A具有Multiple属性.它可以并且确实传递回多个文件.但这不是问题,只有A或B会传回用户上传的文件.

Here is exactly what I want to do that I cannot do right now. I have 2 different file inputs that the user can upload to. I need both file inputs to pass back to the controller. Not just one file input. File input A has the multiple attribute. It can and does pass back multiple files. But that's not the issue is that only A or B will pass back the files that the user uploaded.

public class ViewDesigner 
{
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get; set; }
    public DateTime DateOfBirth { get; set; }      
    public List<File> Files { get; set; }
}


    [HttpPost]
    public ActionResult SignUp(ViewDesigner viewDesigner)
    {

        List<File> Files = new List<File>();
        if (ModelState.IsValid)
        {
            for (int i = 0; i < Request.Files.Count; i++)
            {
                var requestedFile = Request.Files[i];
                File file = new File()
                {
                    FileName = requestedFile.FileName
                };
                Files.Add(file);

            };

            viewDesigner.Files = Files;
        }

        return View();

    }


@using (Html.BeginForm("SignUp", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <b class="col-12">
        Upload up to 5 documents 
    </b>
    <small class="col-12">All files must be uploaded simultaneously.</small>
    <div class="form-group col-lg-12 my-3">
        <input type="file" id="file" name="file" accept="image/*, .gif, .xls, .doc, .docx, .pdf, .ppt" data-multiple-caption="{count} files selected" multiple="multiple" />
        <label for="file"><i class="fad fa-upload"></i> <span>Choose a file&hellip;</span></label>
    </div>


    <b class="col-12">
        Upload one document
    </b>
    <div class="form-group col-lg-12 my-3">
        <input type="file" id="file2" name="file2" accept="image/*, .gif, .xls, .doc, .docx, .pdf, .ppt" />
        <label for="file"><i class="fad fa-upload"></i> <span>Choose a file&hellip;</span></label>
    </div>
}

推荐答案

主要变化

public ActionResult SignUp(IEnumerable<HttpPostedFileBase> file, IEnumerable<HttpPostedFileBase> file2, ViewDesigner viewDesigner)

说明

添加您的cshtml按钮以提交表单

Add in your cshtml button for submit form

@using (Html.BeginForm("SignUp", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <b class="col-12">
        Upload up to 5 documents
    </b>
    <small class="col-12">All files must be uploaded simultaneously.</small>
    <div class="form-group col-lg-12 my-3">
        <input type="file" id="file" name="file" accept="image/*, .gif, .xls, .doc, .docx, .pdf, .ppt" data-multiple-caption="{count} files selected" multiple="multiple" />
        <label for="file"><i class="fad fa-upload"></i> <span>Choose a file&hellip;</span></label>
    </div>


    <b class="col-12">
        Upload one document
    </b>
    <div class="form-group col-lg-12 my-3">
        <input type="file" id="file2" name="file2" accept="image/*, .gif, .xls, .doc, .docx, .pdf, .ppt" />
        <label for="file"><i class="fad fa-upload"></i> <span>Choose a file&hellip;</span></label>
    </div>
    <button type="submit"> submitt</button>
}

按照以下步骤更改您的方法代码

[HttpPost]
public ActionResult SignUp(IEnumerable<HttpPostedFileBase> file, IEnumerable<HttpPostedFileBase> file2, ViewDesigner viewDesigner)
{
    return View();
}

我已经选择了以下图片文件

按如下图所示分别在控制器中获取文件

这篇关于MVC从不同的文件输入上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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