pkonowrocki
pkonowrocki

Reputation: 3

Can't upload file in MVC

I have a problem with uploading files to my database, PostedFile is always a null.

Controller:

public ActionResult Create(Sound sound)
    {
        if (sound.PostedFile != null && sound.PostedFile.ContentLength > 0)
        {
            MemoryStream temp = new MemoryStream();
            sound.PostedFile.InputStream.CopyTo(temp);
            sound.Data = temp.ToArray();
            db.SoundsTable.Add(sound);
            db.SaveChanges();
            ViewBag.Message = "DONE";
        }
        else
        {
            ViewBag.Message = "ERR";
        }
        return RedirectToAction("Index");
    }

Model:

  public class Sounds : DbContext
{
    public Sounds()
        : base("name=Sounds")
    {
    }
    public virtual DbSet<Sound> SoundsTable { get; set; }
}

public class Sound
{
    public int Id { get; set; }
    public string Name { get; set; }
    [MaxLength(8388608)]
    public byte[] Data { get; set; }
    [NotMapped]
    [Required(ErrorMessage = "Please select file.")]
    public HttpPostedFileBase PostedFile { get; set; }

}

View:

@model WebApp.Models.Sound

@{
    ViewBag.Title = "Create";
}
<h2>Create</h2>

@using (Html.BeginForm())
{
    <div class="form-horizontal">
         <h4>Sound</h4>
         @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })<br />
         @Html.TextBoxFor(model => model.PostedFile, new { type = "file" })<br />
         @Html.ValidationMessageFor(model => model.PostedFile, "", new { @class = "error" })<br />
         <input type="submit" value="UPLOAD" />
         <br />
     @ViewBag.Message
    </div>
    <div>
        @Html.ActionLink("Back to List", "Index")
    </div>
}

Everything works fine with Name value being send from View to Controller. But I cant get working sending PostedFile value, as it is always a null.

Upvotes: 0

Views: 899

Answers (2)

Kostas Dafnomilis
Kostas Dafnomilis

Reputation: 629

try this:

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

    <input type="file" name="postedFile" id="fileInputId" />

    //submit button here }

Then your controller:

    [HttpPost]
    public ActionResult Create(Sound model, HttpPostedFileBase postedFile)
    {
        //do stuff
    }

Make sure that the name in input file element is the same as the parameter name in the controller.

Upvotes: 0

Reno
Reno

Reputation: 572

enctype = multipart/form-data is indeed required

@using (Html.BeginForm("Create", //Your action
                           "Test", //Your controller
                           FormMethod.Post,
                           new { enctype = "multipart/form-data"}))
{
    <div class="form-horizontal">
        <h4>Sound</h4>
        @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })<br />
        @Html.TextBoxFor(model => model.PostedFile, new { type = "file" })<br />
        @Html.ValidationMessageFor(model => model.PostedFile, "", new { @class = "error" })<br />
        <input type="submit" value="UPLOAD" />
        <br />
        @ViewBag.Message
    </div>
    <div>
        @Html.ActionLink("Back to List", "Index")
    </div>
} 

Upvotes: 1

Related Questions