c# - 尝试使用 mvc 上传文件时 Request.Files 为空

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

这是我的模型。

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> 

Controller

[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);
} 

最佳答案

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

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

该文件也可以从您的模型中获得,即 libro.File

关于c# - 尝试使用 mvc 上传文件时 Request.Files 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34858814/

相关文章:

c# - 属性更改时属性未将值传回文本框的问题

c# - 如何在 C# 中调用 google.apis.dialogflow.v2

c# - 撤消按钮 VSTO 的所有操作

.net - 为什么使用 Microsoft AntiXSS 库?

ASP.NET - 服务器端控制和 JavaScript 交互

asp.net-mvc - 是否必须在 mvc 中命名 View 和操作方法相同?

c# - Entity Framework : Extending partial class with interface without touching the EF-generated classes

c# - 使用 Razor 按表中的日期对模型元素进行分组

asp.net - 为什么IIS7需要很长时间

c# - web.config 批处理 ="false"