-2

I have tried my best however HttpPostedFileBase filee is always null

Controller Action

 public ActionResult UploadFile(HttpPostedFileBase filee)
        {
            try
            {
                if (filee.ContentLength > 0)
                {
                    string _FileName = Path.GetFileName(filee.FileName);
                    string _path = Path.Combine(Server.MapPath("~/UploadedFiles"), _FileName);
                    filee.SaveAs(_path);
                }
                ViewBag.Message = "File Uploaded Successfully!!";
                return View();
            }
            catch
            {
                ViewBag.Message = "File upload failed!!";
                return View();
            }
        }

Razor View

@{
    ViewBag.Title = "UploadFile";
}

<h2>UploadFile</h2>

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

    <div>
        @Html.TextBox("file", "", new { type = "file" }) <br />
        <input type="submit" value="Upload" />

        @ViewBag.Message

    </div>


}  
John Doe
  • 161
  • 1
  • 2
  • 10
  • 2
    Because the name of the file input is `file` not `filee`. They need to match. –  Dec 29 '17 at 07:20
  • Have a look at [This Answer](https://stackoverflow.com/a/8551621/3814721) – mmushtaq Dec 29 '17 at 07:22
  • Any good reason? for this. We have always have choice to name the instance according to our desire. Employee employee=new Employee(); – John Doe Dec 29 '17 at 07:23
  • Please mention the improvement if you going to vote down my question. It's not enough for a individual to learn what is wrong? By just down voting the question. – John Doe Dec 29 '17 at 07:35
  • @StephenMuecke Thanks for help highly appreciate it. – John Doe Dec 29 '17 at 07:41

2 Answers2

0

Either change the name of parameter in public ActionResult UploadFile(HttpPostedFileBase filee) to public ActionResult UploadFile(HttpPostedFileBase file) or change the input name @Html.TextBox("file", "", new { type = "file" }) to @Html.TextBox("filee", "", new { type = "file" }).

Nitesh Kumar
  • 1,741
  • 4
  • 20
  • 26
0

You have to use same name of yor input field as your HttpPostedFileBase object name while you are working on loosly type view !

Example :

    @{
        ViewBag.Title = "UploadFile";
    }

    <h2>UploadFile</h2>

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

        <div>
            @Html.TextBox("filee", "", new { type = "file" }) <br />
            <input type="submit" value="Upload" />

            @ViewBag.Message

        </div>

}  

Or If you don't want to use same names ? you just have to use a tightly coupled view type view

Tightly
Example:

@model PROJECTNAME.Models.MODELNAME

        @{
            ViewBag.Title = "UploadFile";
        }

        <h2>UploadFile</h2>

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

            <div>
                @Html.TextBox(model => model.YOURCOLUMNNAME , "", new { type = "file" }) <br />
                <input type="submit" value="Upload" />

                @ViewBag.Message

            </div>

    }