0

i have an issue file instance always null , below is the code:

    [HttpPost]
    public ActionResult Create(ExamRegisterationModel model, HttpPostedFileBase file)
    {
        if (file != null && file.ContentLength > 0)
        {
            string path = AppDomain.CurrentDomain.BaseDirectory + "uploads/";
            string filename = Path.GetFileName(Request.Files[0].FileName);
            Request.Files[0].SaveAs(Path.Combine(path, filename));
        }
    }

Any Idea how to upload the file?

And my View :

   <div class="input-group">
     <label for="file">Upload Receipt:</label>
     <input type="file" name="File" id="file" />
     <input type="submit" name="Submit" id="Submit" value="upload" />
   </div>
tereško
  • 58,060
  • 25
  • 98
  • 150

2 Answers2

0

add a wrapper element.

<form action="/Controller/Create" method="post" enctype="multipart/form-data">
    <div class="input-group">
        <label for="file">Upload Receipt:</label>
        <input type="file" name="File" id="file" />
        <input type="submit" name="Submit" id="Submit" value="upload" />
    </div>
</form>
Jason Li
  • 1,528
  • 1
  • 12
  • 20
0

It works after doing the following change:

@using (Html.BeginForm(new { enctype = "multipart/form-data" }))
  • What's the difference between this and my answer ? You didn't change the Controller name in the action attribute? – Jason Li Sep 29 '14 at 08:57