MVC3/EF4.1 Binding HttpPostedFileBase to model and adding to DB

爷,独闯天下 提交于 2019-12-11 18:22:18

问题


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?


回答1:


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");  
  1. You need to adjust the path saved to afterwards, since asp.net won't allow you to serve files from the App_Data folder.
  2. the public ICollection<string> Filenames is just to give you an idea, but you probably need to change that in ICollection<FileEntity> Files.


来源:https://stackoverflow.com/questions/7305460/mvc3-ef4-1-binding-httppostedfilebase-to-model-and-adding-to-db

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!