Database 为什么可以';是否不能以这种方式将图像保存到数据库?
我想将图像和一些其他信息保存到asp.net mc3项目中的数据库中。我以前曾将图像保存到数据库中,效果良好。我的控制器中的代码如下:Database 为什么可以';是否不能以这种方式将图像保存到数据库?,database,asp.net-mvc,asp.net-mvc-3,sql-server-2008,image-uploading,Database,Asp.net Mvc,Asp.net Mvc 3,Sql Server 2008,Image Uploading,我想将图像和一些其他信息保存到asp.net mc3项目中的数据库中。我以前曾将图像保存到数据库中,效果良好。我的控制器中的代码如下: public ActionResult savetodb() { if (Request.Files.Count > 0 && Request.Files[0] != null) { HttpPostedFileBase file = Request.Files[0];
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
返回0。我不知道我现在该怎么办。有人能帮我吗?非常感谢 使用
HttpPostedFileBase
作为操作的参数
如果只发送一个文件,请使用此选项。如果允许多个,则必须使用IEnumerable 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,如果要上载大于4MB的任何内容,则需要在web.config文件中配置设置,向视图模型添加属性以获取此文件:
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;
}
当然,您可以使用相同的属性a图像
,只需将其保存到正确的位置,而无需复制相同类型的缓冲区。我会使用视图模型
假设您首先有一个域模型:
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");
}
}
公共类HomeController:控制器
{
公共行动结果索引()
{
返回视图(新的MyViewModel());
}
[HttpPost]
公共行动结果索引(MyViewModel)
{
如果(!ModelState.IsValid)
{
返回视图(模型);
}
MyDomainModel domainModel=Mapper.Map(模型);
//TODO:通过将域模型传递给方法来持久化该域模型
//在你的DAL层上
返回内容(“感谢提交”);
}
}
最后是允许用户上载文件的视图:
@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>
}
@使用(Html.BeginForm(null,null,FormMethod.Post,new{enctype=“multipart/form data”}))
{
@LabelFor(x=>x.Description)
@EditorFor(x=>x.Description)
@LabelFor(x=>x.File)
@Html.TextBoxFor(x=>x.File,新的{type=“File”})
@Html.ValidationMessageFor(x=>x.File)
好啊
}
您是否在HTML表单上设置了enctype
您能提供您的视图片段吗?我还建议不要将文件物理存储在服务器上,也不要存储在站点本身中。直接将其存储在数据库中即可。这使得你的网站没有任何上传,而且它还与农场环境中的部署兼容。我认为这是一个it技术问题,不一定是开发问题。如果图像数量和大小很大,那么在DB中不是一个好的解决方案,除非IT部门有一个很好的杀手级fast集群和fastSANs@AlfalfaStrange:谢谢你的回答和忠告。我尝试了这个代码,但我不知道为什么不起作用。看起来是对的。在我的数据库中,图像值为空。@Tena您首先将图像保存到文件系统,在尝试保存到DB之前,它是否在物理上存在?@Alfalfastange:不,它不是。我检查了,在这段代码中,file的值为NULL。当我只想保存图像时(对于一个只有一列用于图像的表),它是有效的,但是当我想使用其他信息,如我上面描述的代码时,它是无效的。