【问题标题】:MVC3/EF4.1 Binding HttpPostedFileBase to model and adding to DBMVC3/EF4.1 将 HttpPostedFileBase 绑定到模型并添加到 DB
【发布时间】:2011-09-05 08:17:13
【问题描述】:

我有一个将数据绑定到模型并将其添加到数据库的 ActionResult。现在我想要的是有一个文件上传器和 ActionResult 来存储文件并将它们的 FileName 添加到数据库中(这样我可以稍后显示文件/图像)。最好的方法是什么?这是我目前得到的(它可以存储文件,但我不确定 EF 有多智能,数据类型有多复杂):

模型类:

    public class Annonce
{
    public int Id { get; set; }
    public string Company { get; set; }
    public string Size { get; set; }
    public IEnumerable<HttpPostedFileBase> FileUpload { get; set; }
}

视图(使用 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>

控制器:

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

这可以很好地存储文件。现在我想要的是file.FileName 存储在数据库中。起初,我虽然 EF 只是将文件或文件名从 model.FileUpload 绑定到数据库。但我猜数据类型太复杂了?所以我想制作一个list&lt;HttpPostedFileBase&gt; 并在那里添加file.FileName?或者可能创建一个全新的实体/表,其中所有文件名都存储为带有引用 ID 的字符串? 有什么建议吗?

【问题讨论】:

    标签: asp.net-mvc-3 entity-framework-4.1 ef-code-first httppostedfilebase


    【解决方案1】:

    我认为您不想将实际文件内容保存在数据库中。 所以我会做这样的事情:

    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");  
    
    1. 您需要在之后调整保存到的路径,因为 asp.net 不允许您从 App_Data 文件夹中提供文件。
    2. public ICollection&lt;string&gt; Filenames 只是给您一个想法,但您可能需要在ICollection&lt;FileEntity&gt; Files 中更改它。

    【讨论】:

    • 没错,我不希望将文件存储在数据库中。只是文件名。但是是的 ICollection 不会工作,我需要 HttpPostedFileBase 来保存文件和东西。我不确定它与 ICollection 而不是 IEnumerable 有什么区别?还是您建议我在 FileUpload 旁边创建另一个名为 Filenames 的属性?
    • 我的意思应该是实体的集合,而不仅仅是字符串。您的 Annonce 实体与上传的文件之间存在 1:n 关系。
    • 这就够了(见答案)?
    • 是的。谢谢你。不确定这就是你的意思。
    猜你喜欢
    • 2013-02-08
    • 1970-01-01
    • 2013-12-24
    • 1970-01-01
    • 2017-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多