为什么不能以这种方式将图像保存到数据库?

时间:2012-06-10 17:59:13

标签: database asp.net-mvc asp.net-mvc-3 sql-server-2008 image-uploading

我想在我的asp.net mc3项目中将Image和其他一些信息保存到数据库中。我之前已将Image保存到数据库并且工作正常。我的控制器中的代码是这样的:

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

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

 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

返回0.我不知道现在该怎么办。有人能帮帮我吗?非常感谢。

3 个答案:

答案 0 :(得分:3)

我会使用视图模型。

我们假设您首先拥有域模型:

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>
}

答案 1 :(得分: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文件中配置设置所需的内容

答案 2 :(得分: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并将其保存到正确的位置,而不会复制缓冲区(如果它的类型相同)。