MVC3 / EF4.1绑定HttpPostedFileBase模型并添加到数据库 [英] MVC3/EF4.1 Binding HttpPostedFileBase to model and adding to DB
问题描述
我有一个结合数据模型,并增加了它的DB一个ActionResult。现在,我要的,是有一个文件上传,和的ActionResult来存储文件,并加入自己的FileName为DB(这样我就可以显示文件/在以后的图像)。什么是最好的方法呢?以下是我走到这一步,(它可以存储文件,但林不知道怎么EF智能就是和数据类型多么复杂均可):
模型类:
公共类Annonce
{
公众诠释标识{搞定;组; }
公共字符串公司{搞定;组; }
公共字符串大小{搞定;组; }
公共IEnumerable的< HttpPostedFileBase>文件上传{搞定;组; }
}
该视图(使用mircosoft.web.helpers):
@using(Html.BeginForm(创建,Annonce,FormMethod.Post,新{ENCTYPE =的multipart / form-data的}))
{
@ Html.ValidationSummary(真)
<&字段集GT;
<传奇> Annonce< /传说> < DIV CLASS =编辑标记>
@ Html.LabelFor(型号=> model.Company)
< / DIV>
< DIV CLASS =主编场>
@ Html.EditorFor(型号=> model.Company)
@ Html.ValidationMessageFor(型号=> model.Company)
< / DIV> < DIV CLASS =编辑标记>
@ Html.LabelFor(型号=> model.Size)
< / DIV>
< DIV CLASS =主编场>
@ Html.EditorFor(型号=> model.Size)
@ Html.ValidationMessageFor(型号=> model.Size)
< / DIV> < DIV CLASS =编辑标记>
@ Html.LabelFor(型号=> model.FileUpload)
< / DIV>
< DIV CLASS =主编场>
@ FileUpload.GetHtml(uploadText:啊,preT)
< / DIV>
< /字段集>
控制器:
[HttpPost]
公众的ActionResult创建(Annonce annonce)
{
如果(ModelState.IsValid)
{
//System.IO东西
的foreach(在annonce.FileUpload var文件)
{
如果(file.ContentLength大于0)
{
变种文件名= Path.GetFileName(file.FileName);
VAR路径= Path.Combine(使用Server.Mappath(〜/ App_Data文件/上传),文件名);
file.SaveAs(路径); }
}
//数据库的东西
db.Annoncer.Add(annonce);
db.SaveChanges();
返回RedirectToAction(「指数」);
} 返回查看(annonce);
}
这所存储的文件就好了。现在,我想要的东西,是 file.FileName
是在DB存储。起初,我虽然EF只会将文件名或文件名从 model.FileUpload
绑定到数据库。但我猜的数据类型太复杂?所以,我虽然制作列表&LT的; HttpPostedFileBase>
并添加 file.FileName
吗?或者,也许创造一个全新的实体/表,其中所有的文件名保存为一个参考标识字符串?
有什么建议?
我不认为你要保存实际的文件内容在数据库中。
所以,我会做这样的事情:
公共类AnnonceFile
{
公共字符串的文件名{获得;设置;}
}公共类Annonce
{
公众诠释标识{搞定;组; }
公共字符串公司{搞定;组; }
公共字符串大小{搞定;组; }
公众的ICollection< AnnonceFile>文件名{搞定;组; }
}
//System.IO东西
的foreach(在文件var文件)
{
如果(file.ContentLength大于0)
{
变种文件名= Path.GetFileName(file.FileName);
VAR路径= Path.Combine(使用Server.Mappath(〜/ App_Data文件/上传),文件名);
file.SaveAs(路径);
annonce.Filenames.Add(新AnnonceFile {名=路径}); }
}
//数据库的东西
db.Annoncer.Add(annonce);
db.SaveChanges();
返回RedirectToAction(「指数」);
- 您需要调整保存后的路径,因为asp.net不会让你从App_Data文件夹中的文件服务。
- 在
公开的ICollection<串GT;文件名
只是给你一个想法,但你可能需要改变,在的ICollection< FileEntity>文件
。
I have an ActionResult which binds data to the model and adds it the the DB. Now what I want, is to have a file uploader, and the ActionResult to store the files and adding their FileName to the DB (so I can show the files/images at a later point). What is the best approach? Here is what I got so far (It can store the files, but im not sure how intelligent EF is, and how complex the datatypes can be):
The model class:
public class Annonce
{
public int Id { get; set; }
public string Company { get; set; }
public string Size { get; set; }
public IEnumerable<HttpPostedFileBase> FileUpload { get; set; }
}
The view (using mircosoft.web.helpers):
@using (Html.BeginForm("Create", "Annonce", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.ValidationSummary(true)
<fieldset>
<legend>Annonce</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Company)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Company)
@Html.ValidationMessageFor(model => model.Company)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Size)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Size)
@Html.ValidationMessageFor(model => model.Size)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.FileUpload)
</div>
<div class="editor-field">
@FileUpload.GetHtml(uploadText: "Opret")
</div>
</fieldset>
The controller:
[HttpPost]
public ActionResult Create(Annonce annonce)
{
if (ModelState.IsValid)
{
//System.IO stuff
foreach (var file in annonce.FileUpload)
{
if (file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
file.SaveAs(path);
}
}
//Database stuff
db.Annoncer.Add(annonce);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(annonce);
}
This stores the files just fine. Now what I want, is file.FileName
to be stores in the db. At first, I though EF would simply bind the files or filenames from model.FileUpload
to the db. But I guess the datatype is too complex? So I though of making a list<HttpPostedFileBase>
and adding the file.FileName
there? Or maybe create an entire new entity/table where all the filenames are stored as strings with a reference-Id?
Any suggestions?
I don't think you want to save the actual file contents in the database. So I would do something like:
public class AnnonceFile
{
public string filename {get;set;}
}
public class Annonce
{
public int Id { get; set; }
public string Company { get; set; }
public string Size { get; set; }
public ICollection<AnnonceFile> Filenames{ get; set; }
}
//System.IO stuff
foreach (var file in files)
{
if (file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
file.SaveAs(path);
annonce.Filenames.Add( new AnnonceFile {filename = path});
}
}
//Database stuff
db.Annoncer.Add(annonce);
db.SaveChanges();
return RedirectToAction("Index");
- You need to adjust the path saved to afterwards, since asp.net won't allow you to serve files from the App_Data folder.
- the
public ICollection<string> Filenames
is just to give you an idea, but you probably need to change that inICollection<FileEntity> Files
.
这篇关于MVC3 / EF4.1绑定HttpPostedFileBase模型并添加到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!