Click here to Skip to main content
15,912,897 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
public ActionResult test()
       {
           return View();
       }

       [HttpPost]
       public ActionResult test(HttpPostedFileBase file)
       {
           if (file == null)
           {
               return View("empty");
           }
           else
           if (file != null && file.ContentLength > 0)
           {

               var fileName = Path.GetFileName(file.FileName);

               var path = Path.Combine(Server.MapPath("~/image/"), fileName);
               file.SaveAs(path);
               return View();
           }
           else {
             return  View("ok");

           }

           return RedirectToAction("Index");
       }






@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>test</title>
</head>
<body>
    <div> 
        @ViewBag.msg;
@using (Html.BeginForm("test", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
        <input type="file" id="file" />
        <input type="submit" />
        }
    </div>
</body>
</html>


What I have tried:

i am trying to upload the image in my image folder of 150kb size but it shows empty(empty file) view or nullreference like exception .
Posted
Updated 7-Feb-20 3:59am

1 solution

Try giving the input a name attribute of "file"

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



Uploading a File (Or Files) With ASP.NET MVC[^]
 
Share this answer
 
v2
Comments
Vivek Kansal 7-Feb-20 10:11am    
thanks man alot...it worked

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900