在MVC4文件中上传它在DB中保存“System.Web.HttpPostedFileWrapper”而不是文件名

时间:2013-12-30 04:46:39

标签: asp.net-mvc-4 c#-4.0 file-upload

我正在尝试使用MVC4进行文件上传,但是在DB中保存对象名称“System.Web.HttpPostedFileWrapper”而不是文件名即“Songs.MP3”,同时文件也不会传输到给定位置。< / p>

MODEL

   public class FileUpload
{
    [Key]
    public int FileUploadID { get; set; }
    public int AlbumID { get; set; }
    public string FileType { get; set; }
    public string FileUploadLocation { get; set; }

    public virtual Albums Albums { get; set; }
}

查看

@using (Html.BeginForm("Create", "FileUpload", FormMethod.Post, new { enctype = "multipart/form-data" }))

{


    <div class="editor-label">
        @Html.LabelFor(model => model.FileUploadLocation)
    </div>
    <div class="editor-field">
        @Html.TextBoxFor(model => model.FileUploadLocation, new { type = "file", accept = "FileUploadLocation/*" })
        @Html.ValidationMessageFor(model => model.FileUploadLocation)
    </div>

控制器

        //
    // POST: /FileUpload/Create

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create(FileUpload fileupload, HttpPostedFileBase FileUploadLocation)
    {
        if (ModelState.IsValid)
        {
            var fileName = Path.GetFileName(FileUploadLocation.FileName);
            var path = Path.Combine(Server.MapPath("~/Images/Files"), fileName);
            FileUploadLocation.SaveAs(path);

            db.FileUploads.Add(fileupload);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        ViewBag.AlbumID = new SelectList(db.Albumss, "AlbumID", "AlbumTitle", fileupload.AlbumID);
        return View(fileupload);
    }

文件位于〜/ Images / Files位置。

1 个答案:

答案 0 :(得分:0)

这里几乎没有问题。第一个问题是命名约定。您的FileUpload模型将属性FileUploadLocation作为字符串,并且在控制器的Create方法中,您将传递FileUpload fileupload模型和HttpPostedFileBase FileUploadLocation

其他更重要的问题是您不应该将View Model保存到数据库,它应该映射到某种域对象,而这些域对象又会被保存。例如:

创建新的视图模型:

public class FileUploadViewModel
{
    public int FileUploadID { get; set; }
    public int AlbumID { get; set; }
    public string FileType { get; set; }
    public HttpPostedFileBase FileUploadFile { get; set; }

    public virtual Albums Albums { get; set; }
}

从您的域模型中删除虚拟方法:

public class FileUpload
{
    [Key]
    public int FileUploadID { get; set; }
    public int AlbumID { get; set; }
    public string FileType { get; set; }
    public string FileUploadLocation { get; set; }
}

然后您的Controller Create方法应该如下所示:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(FileUploadViewModel model)
{
    if (ModelState.IsValid)
    {
         var fileName = Path.GetFileName(model.FileUploadFile.FileName);
         var path = Path.Combine(Server.MapPath("~/Images/Files"), fileName);
         model.FileUploadFile.SaveAs(path);

         db.FileUploads.Add(new FileUpload
         {
             FileUploadID = model.FileUploadID,
             AlbumID = model.AlbumID,
             FileType = model.FileType,
             FileUploadLocation = path
         });
         db.SaveChanges();
         return RedirectToAction("Index");
    }

        ViewBag.AlbumID = new SelectList(db.Albumss, "AlbumID", "AlbumTitle", model.AlbumID);
        return View(model);
    }