【问题标题】:Upload multiple files with single input Asp Mvc使用单个输入 Asp Mvc 上传多个文件
【发布时间】:2017-08-09 20:16:17
【问题描述】:

我想上传多个文件,包括 Word 文档、Pdf 和图像。 我需要对文件使用单个输入,因为我们不知道将上传多少文件。

我的代码是这样的,但是我遇到了无法将文件发送到服务器端的问题。

控制器代码:

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>

我在保存文件和保存对象方面没有问题。 唯一的问题: 文件列表为空。

【问题讨论】:

  • 显示你的&lt;form&gt;元素
  • 我添加了这个问题。

标签: c# jquery ajax asp.net-mvc file-upload


【解决方案1】:

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

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

或更好

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

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

【讨论】:

    猜你喜欢
    • 2014-01-03
    • 2014-09-26
    • 2023-02-12
    • 2015-12-22
    • 1970-01-01
    • 2012-04-08
    • 2020-07-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多