简体   繁体   English

ASP.NET MVC 上传图片到 model

[英]ASP.NET MVC upload images to a model

I have a little problem with adding images in ASP.NET MVC.我在 ASP.NET MVC 中添加图像时遇到了一点问题。 I created a folder in my root folder named /Images/Brands/ where I wanted to save images of my brand models.我在我的根文件夹中创建了一个名为/Images/Brands/的文件夹,我想在其中保存我的品牌模型的图像。

This is my Brand model:这是我的Brand model:

namespace AspProjectBootstrap.Models
{
    public class Brand
    {
        public int ID { get; set; }
        [Required]
        public String Name { get; set; }
        [Required]
        public String Description { get; set; }
        public virtual ICollection<Product> Products { get; set; }
        public String ImagePath { get; set; }
        [NotMapped]
        public HttpPostedFileBase ImageFile { get; set; }
    }
}

I saw some people saving the path in the models class as ImagePath then when needed get the full path to the images from the ImagesPath .我看到有些人将模型 class 中的路径保存为ImagePath ,然后在需要时从ImagesPath获取图像的完整路径。

And this is my create brand function:这是我创建的品牌 function:

public ActionResult Create(Brand brand)
{
    string filename = Path.GetFileName(brand.ImageFile.FileName);
    filename = DateTime.Now.ToString("yymmssfff") + filename+".png";
    brand.ImagePath = "~/Images/Brands/" + filename;
    filename = Path.Combine(Server.MapPath("~/Images"), filename);
    brand.ImageFile.SaveAs(filename);

    if (ModelState.IsValid)
    {
        db.Brands.Add(brand);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

    return View(brand);
}

But I get an error.但我得到一个错误。

If you have any idea how to solve this problem, or any other method to do this task.如果您有任何想法如何解决此问题,或任何其他方法来完成此任务。

Thank you in advance.先感谢您。

The error is:错误是:

在此处输入图像描述

The create Create.cshtml view is:创建Create.cshtml视图是:

<div class="form-group">
    @Html.LabelFor(model => model.ImagePath, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        <input type="file" name="ImageFile" />
    </div>
</div>

Your Create method is accepting a serialized model, and while the model can contain a file data, chances are it is not resolving the name:您的 Create 方法正在接受序列化的 model,虽然 model 可以包含文件数据,但它可能无法解析名称:

This should work:这应该有效:

@Html.TextBoxFor(model => model.ImageFile, new { type="file" })

This will ensure the resulting input is named so MVC will recognize it on the form submit or serializing the model.这将确保生成的输入被命名,以便 MVC 在表单提交或序列化 model 时识别它。

If that isn't working the next thing to check is to put a breakpoint on string filename = Path.GetFileName(brand.ImageFile.FileName);如果这不起作用,接下来要检查的是在string filename = Path.GetFileName(brand.ImageFile.FileName);上放置一个断点。 and inspect which part is #null.并检查哪个部分是#null。 Is ImageFile null or ImageFile.FileName ?ImageFile null 还是ImageFile.FileName You should also validate your code to check for nulls in case a form can be submitted without supplying a file.您还应该验证您的代码以检查空值,以防可以在不提供文件的情况下提交表单。

The problem was in the Create.cshtml view:问题出在 Create.cshtml 视图中:

@using (Html.BeginForm())

The solution was:解决方案是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM