0

My html file index.cshtml is like this

<html>
<head>
</head>
<body>
    <form method="post">
        <input type="file" name="myfile" id="myfile" />
        <input type="submit" value="Upload" />
    </form>
</body>
</html>

My controller is like this

public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Index(HttpPostedFileBase myfile)
        {
            string currentdir = Directory.GetCurrentDirectory();

            myfile.SaveAs(currentdir + "\\" + myfile.FileName);

            return View();
        }
    }

And the error occurred when I posted a file. It told myfile object was null. Please help fixing this. Thx so much !

1
  • Using enctype="multipart/form-data" in your form tag Commented Oct 16, 2018 at 10:08

2 Answers 2

2

Add enctype attribute in form tag like below,

<form method="post" enctype="multipart/form-data">
Sign up to request clarification or add additional context in comments.

Comments

1
<html>
<head>
</head>
<body>
    <form method="post" enctype="multipart/form-data">
        <input type="file" name="myfile" id="myfile" />
        <input type="submit" value="Upload" />
    </form>
</body>
</html>

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.