0

I'm attempting to upload an xml file to my site. However, regardless of which file I attempt to upload, the HttpPostedFileBase element in my code is null. I don't understand why this is. I've followed all the examples I can find on uploading files and it doesn't seem to make any sense. This is the controller method

[HttpPost]
    public ActionResult UploadFile(HttpPostedFileBase xmlFile)
    {
        if (xmlFile != null && xmlFile.ContentLength > 0)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(xmlFile.InputStream);
            // other logic later
            return RedirectToAction("Index");
        }
        return RedirectToAction("UploadFailed");    
    }

and the cshtml:

@{
ViewBag.Title = "Upload";
}

@using (Html.BeginForm("UploadFile", "Home", FormMethod.Post, new { enctype = "multipart/form-data"   }))
{
<input type="file" name="file" />
<input type="submit" value="OK" />

}

1 Answer 1

2

It has a wrong name. The action argument is called xmlFile whereas your file input is called file. You need to be consistent in your naming conventions:

<input type="file" name="xmlFile" />

I also invite you to checkout Phil Haack's blog post on this subject.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.