【问题标题】:MVC HttpPostedFileBase always nullMVC HttpPostedFileBase 始终为空
【发布时间】:2014-02-24 20:47:00
【问题描述】:

我有这个控制器,我想做的是将图像作为 [byte] 发送到控制器,这是我的控制器:

[HttpPost]
public ActionResult AddEquipment(Product product, HttpPostedFileBase image) 
 {

            if (image != null) 
            {
                product.ImageMimeType = image.ContentType;
                product.ImageData = new byte[image.ContentLength];
                image.InputStream.Read(product.ImageData, 0, image.ContentLength);
            }

            _db.Products.Add(product);
            _db.SaveChanges();

            return View();
 }

在我看来:

@using (Html.BeginForm("AddEquipment", "Equipment", FormMethod.Post)) {

    <fieldset>
        <legend>Product</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.Name)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Name)
            @Html.ValidationMessageFor(model => model.Name)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Description)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Description)
            @Html.ValidationMessageFor(model => model.Description)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Price)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Price)
            @Html.ValidationMessageFor(model => model.Price)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Category)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Category)
            @Html.ValidationMessageFor(model => model.Category)
        </div>

        <div>
        <div>IMAGE</div>
        <input type="file" name="image" />

        </div>


        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

但问题是在我的控制器上,图像的值始终为空,我似乎无法获得有关 HttpPostedFileBase 的任何信息

【问题讨论】:

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


    【解决方案1】:

    您需要使用 multipart/form-data 添加encType

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

    如果它是 ViewModel,您也可以随时将其添加到您的模型中:

    public class Product 
    {
        public Product()
        {
            Files = new List<HttpPostedFileBase>();
        }
    
        public List<HttpPostedFileBase> Files { get; set; }
        // Rest of model details
    }
    

    您可以通过删除不需要的参数来检索文件,即

    [HttpPost]
    public ActionResult AddEquipment(Product product) 
     {
        var file = model.Files[0];
        ...
     }
    

    【讨论】:

    • @RiquelmyMelara 没有问题,我也会改进答案。
    • 我猜他的“产品”对象是一个实体框架“实体”类型,所以添加另一个属性可能会破坏他的代码,除非他也修复了配置
    • @Kaz-LA 是的,如果它不是 ViewModel,在这种情况下,创建一个新的 ViewModel 并添加它很容易。
    • 是的,新的 {enctype = "multipart/form-data" } 成功了
    【解决方案2】:

    尝试在 Action 方法的顶部执行此操作:

    [HttpPost]
        public ActionResult AddEquipment(Product product, HttpPostedFileBase image)
        {
            image = image ?? Request.Files["image"];
            // the rest of your code
        }
    

    并且表单应该具有“multipart/form-data”的enctype来上传文件:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多