Question

I am using MVC 4 and I tried for upload file concept.

Here is my code:

<div class="complianceSubDiv">
  <div class="complianceLeftDiv">
    @Html.Label("Upload the file")
  </div>
  <div class="complianceRightDiv">
    <input type="file" id="file" name="file" />
  </div>
</div>

My controller code like

[HttpPost]
public ActionResult ManageDocument(DocumentModel documentModel, HttpPostedFileBase file)
{
    //some code
}

But the HttpPostedFileBase file always returns null. I have searched more answers in StackOverflow and other websites and I got the working answer is parameter of HttpPostedFileBase variable name and fileupload control name are same . So I put the same name on all sides, but it returns null only.

Anyone help to me?

Was it helpful?

Solution

Finally i got it

Now i replaced for @using (Html.BeginForm())

to

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

It's working !

OTHER TIPS

    [HttpPost]
    public ActionResult FileUpload(HttpPostedFileBase myFile)
    {
        myFile = Request.Files["file"];
        if (myFile != null && myFile.ContentLength > 0)
        {
            // your code ....
        }
        return View();
    }

You can use "Request.Files" to get the selected file, above is the code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top