MVC3/EF4.1 Binding HttpPostedFileBase to model and

2019-08-21 01:43发布

问题:

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.