使用一个输入的Asp Mvc上载多个文件 [英] Upload multiple files with single input Asp Mvc

查看:47
本文介绍了使用一个输入的Asp Mvc上载多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想上传多个文件,包括Word文档,PDF和图像. 我需要为文件使用一个输入,因为我们不知道将上传多少文件.

I want to upload multiple files including Word Documents, Pdf and Images. I need to use a single input for files because we don't know how many files will be uploaded.

我的代码是这个,但是我无法将文件发送到服务器端.

My code is this but I have problem that I can't send files to server side.

控制器代码:

public ActionResult Create([Bind(Include = "Id,Content")] Message message, IEnumerable<HttpPostedFileBase> files)
    {

        if (ModelState.IsValid)
        {
        // Object save logic
        SaveFiles(message,files);
        return RedirectToAction("Index");
        }
        return View(message);
    }

部分查看代码:

<form name="registration"  action="">
            @Html.ValidationSummary(true, "", new { @class = "text-danger" })
            <div class="form-group">
                <label for="Content" >Content:</label>
                <textarea class="form-control compose_content" rows="5" id="Content" name="content"></textarea>

                  <div class="fileupload">
                      Attachment :  

                      <input id="files" name="files" type="file" multiple />
                  </div>
            </div>
            <button type="submit" class="btn btn-default compose_btn" >Send Message</button>
        </form>

保存文件和保存对象没有问题. 唯一的问题: 文件列表为空.

I don't have problem with saving files and saving the object. only problem : files list is null.

推荐答案

我要上传文件,您的表单需要包含enctype= multipart/form-data属性.

I order to upload files, your form needs to include the enctype= multipart/form-data attribute.

<form name="registration"  action="" enctype= "multipart/form-data">

或更好

@using (Html.BeginForm(actionName, controllerName, FormMethod.Post, new { enctype= "multipart/form-data" }))

并且我强烈建议您将模型传递给视图,并使用强类型的HtmlHelper方法为模型的属性创建html.

and I strongly recommend you pass a model to the view and use the strongly typed HtmlHelper methods to create your html for the properties of your model.

这篇关于使用一个输入的Asp Mvc上载多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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