2

我想将图像和其他一些信息保存到我的 asp.net mc3 项目中的数据库中。我之前已经将图像保存到数据库并且它有效。我在控制器中的代码是这样的:

public ActionResult savetodb()
{
    if (Request.Files.Count > 0 && Request.Files[0] != null)
        {
             HttpPostedFileBase file = Request.Files[0];
             var path = Path.Combine(Server.MapPath("~/Content/Image"), file.FileName); 
             file.SaveAs(path);
             byte[] buffer = System.IO.File.ReadAllBytes(path);
             myAd.AdImage = buffer;
             StoreDb.AddToAds(myAd);
             StoreDb.SaveChanges();
        }
        return View();      
    }
}

现在我更改了表格并希望将图像以外的其他信息保存到数据库中。现在我的代码是这样的:

 public ActionResult savetodb(AdvertiseView model)
 {
     if (Request.Files.Count > 0 && Request.Files[0] != null)
     {
         HttpPostedFileBase file = Request.Files[0];
         var path = Path.Combine(Server.MapPath("~/Content/Image"), file.FileName);
         file.SaveAs(path);
         byte[] buffer = System.IO.File.ReadAllBytes(path);
         myAd.AdImage = buffer;
     }
     myAd.AdTitle = model.AdTitle;
     myAd.AdContext = model.context;
     myAd.AdScope = model.Scope;
     storedb.AddToAds(myAd);
     storedb.SaveChanges();
     return View();
}

其他信息没有任何问题,但图像无法保存。我明白那个

Request.Files.Count

return 0. 我不知道我现在该怎么办。有人可以帮我吗?非常感谢。

4

3 回答 3

4

我会使用视图模型。

假设您首先有一个域模型:

public class MyDomainModel
{
    public byte[] AdImage { get; set; }
    public string Description { get; set; }
}

然后定义一个视图模型:

public class MyViewModel
{
    [Required]
    public HttpPostedFileBase File { get; set; }

    [DataType(DataType.MultilineText)]
    public string Description { get; set; }
}

控制器:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View(new MyViewModel());
    }

    [HttpPost]
    public ActionResult Index(MyViewModel model)
    {
        if (!ModelState.IsValid)
        {
            return View(model);
        }

        // TODO: move this mapping logic into a 
        // mapping layer to avoid polluting the controller
        // I would recommend AutoMapper for this purpose
        // http://automapper.org/
        using (var stream = new MemoryStream())
        {
            model.File.InputStream.CopyTo(stream);
            var image = stream.ToArray();
            var domainModel = new MyDomainModel
            {
                AdImage = image,
                Description = model.Description
            };

            // TODO: persist the domain model by passing it to a method
            // on your DAL layer
        }

        return Content("Thanks for submitting");
    }
}

一旦推荐的重构完成:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View(new MyViewModel());
    }

    [HttpPost]
    public ActionResult Index(MyViewModel model)
    {
        if (!ModelState.IsValid)
        {
            return View(model);
        }

        MyDomainModel domainModel = Mapper.Map<MyViewModel, MyDomainModel>(model);

        // TODO: persist the domain model by passing it to a method
        // on your DAL layer

        return Content("Thanks for submitting");
    }
}

最后是允许用户上传文件的视图:

@using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <div>
        @Html.LabelFor(x => x.Description)
        @Html.EditorFor(x => x.Description)
    </div>

    <div>
        @Html.LabelFor(x => x.File)
        @Html.TextBoxFor(x => x.File, new { type = "file" })
        @Html.ValidationMessageFor(x => x.File)
    </div>    
    <button type="submit">OK</button>
}
于 2012-06-10T18:41:32.270 回答
2

用作HttpPostedFileBase动作的参数。

如果您只发送一个文件,请使用此选项。如果您允许多个,那么您必须使用IEnumerable<HttpPostedFileBase> files作为参数。

public ActionResult savetodb(HttpPostedFileBase file)
{
    if(file != null)
    {
        var path = Path.Combine(Server.MapPath("~/Content/Image"), file.FileName);
        file.SaveAs(path);
        byte[] buffer = System.IO.File.ReadAllBytes(path);
        myAd.AdImage = buffer;
        StoreDb.AddToAds(myAd);
        StoreDb.SaveChanges();
    }
    return View();      
}

您还必须确保您的表单正确构建在您的视图中

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

另请注意,默认情况下,浏览器文件大小上传限制为 4MB,如果您想上传大于该大小的任何内容,则需要在 web.config 文件中配置您的设置

于 2012-06-10T18:16:32.387 回答
1

向您的视图模型添加一个属性以获取此文件:

public class AdvertiseView
{
    ...    
    public HttpPostedFileBase NameOfFileInput;
    ....
}

因此,您可以将文件作为模型的属性获取:

if (myAd.NameOfFileInput != null)
{
    var path = Path.Combine(Server.MapPath("~/Content/Image"), myAd.NameOfFileInput.FileName);
    myAd.NameOfFileInput.SaveAs(path);
    byte[] buffer = System.IO.File.ReadAllBytes(path);
    myAd.AdImage = buffer;
}

当然,您可以使用相同的属性AdImage并将其保存到正确的位置,如果缓冲区的类型相同,则无需复制缓冲区。

于 2012-06-10T18:16:48.537 回答