In MVC4 file upload its saving "System.Web.HttpPostedFileWrapper" in DB instead of file name

Viewed 6692

I'm trying to do a file-upload using MVC4 but its saving object name "System.Web.HttpPostedFileWrapper" in DB instead of file name i.e. "Songs.MP3", also file is not transferred to given location.

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

View

@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>

Controller

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

file is not available in ~/Images/Files location.

1 Answers
Related