我有一个将数据绑定到模型并将其添加到DB的动作冲突。现在,我想要的是要有一个文件上传器,而ActionResult存储文件并将其文件名添加到数据库中(因此,我可以在以后显示文件/图像)。最好的方法是什么?这是我到目前为止得到的(它可以存储文件,但我不确定智能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 成为DB中的商店。首先,我只是EF只需绑定文件或文件名 model.FileUpload 到DB。但是我想数据类型太复杂了吗?所以我虽然做一个 list<HttpPostedFileBase> 并添加 file.FileName 那里?还是创建一个全新的实体/表格,将所有文件名存储为带有参考ID的字符串?有什么建议么?

有帮助吗?

解决方案

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

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<string> Filenames 只是为了给您一个主意,但您可能需要在 ICollection<FileEntity> Files.
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top