【问题标题】:Request.Files is empty when trying to upload file with mvc尝试使用 mvc 上传文件时,Request.Files 为空
【发布时间】:2016-01-18 15:52:29
【问题描述】:

这是我的模型。

public class Libro
{
    [Key]
    [ScaffoldColumn(false)]
    public int ID { get; set; }

    [Required]
    [MaxLength(300, ErrorMessage = "El Path debe tener como máximo 300 caractéres")]
    [Display(Name = "Ruta de acceso")]
    public string Path { get; set; }

    [Required]
    [MaxLength(80, ErrorMessage = "El nombre debe tener como máximo 80 caractéres")]
    public string Nombre { get; set; }

    [Required]
    [Display(Name = "Ruta de acceso")]
    public HttpPostedFileBase File { get; set; }

    //Relations
    public virtual ICollection<Hoja> Hojas { get; set; }
}

这是我的看法:

@model xx.Models.Libro

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>

@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>Libro</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.File, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.TextBoxFor(x => x.File, new { type = "file" })
                @*@Html.EditorFor(model => model.File, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.File, "", new { @class = "text-danger" })*@
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Nombre, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Nombre, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Nombre, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div> 

控制器

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Nombre")] Libro libro)
{
    if (ModelState.IsValid)
    {
        foreach (string upload in Request.Files)
        {
            if (Request.Files[upload].ContentLength == 0) continue;
            string pathToSave = Server.MapPath("~/App_Data/Uploads");
            string filename = Path.GetFileName(Request.Files[upload].FileName);
            Request.Files[upload].SaveAs(Path.Combine(pathToSave, filename));
            libro.Path = Path.Combine(pathToSave, filename);
            db.Libro.Add(libro);
            db.SaveChanges();
        }
        return RedirectToAction("Index");
    }

    return View(libro);
} 

【问题讨论】:

  • 您是否使用 Fiddler 检查所有内容是否以您期望的格式发送?
  • 我确定这个问题之前有人问过。

标签: c# asp.net asp.net-mvc asp.net-mvc-4 razor


【解决方案1】:

需要将表单上的encType设置为multipart/form-data

@using (Html.BeginForm("Create", "ControllerName", FormMethod.Post, 
new { encType="multipart/form-data" }))

该文件也将在您的模型中可用,即libro.File

【讨论】:

    【解决方案2】:

    尝试将您的 BeginForm 更改为:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-02
      • 2013-07-16
      • 1970-01-01
      • 2018-12-30
      • 1970-01-01
      • 2012-02-13
      • 2010-10-17
      • 2015-04-12
      相关资源
      最近更新 更多