prerak desai
prerak desai

Reputation: 171

HttpPostedFileBase retun null always in controller post method

Here My cshtml.cs file for upload a image and privew a image for my site.

@using (Html.BeginForm("Logo", "Header", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <div class="col-md-12">
        <div class="panel">
            <div class="panel-heading">
                <h2>Logo</h2>
            </div>
            <div class="panel-body">
                <div class="col-md-6">
                    <div class="form-group">
                        <label>Logo Image<span style="color:red;">*</span>:</label>
                        @Html.HiddenFor(model => model.logoId)
                        @Html.TextBoxFor(model => model.LogoImage, new { @type = "file", @Id = "Files" })
                        @Html.ValidationMessageFor(model => model.LogoImage)
                    </div>
                    <span style="color:red;">Note:Please Upload Maximum Image size is 6 MB!!</span><br />
                    <span style="color:red;">Note:Allowed Image size Height: 45px, Width: 192px</span>
                    <div class="form-group">
                        <input type="submit" name="submit" value="Upload" class="btn btn-sm btn-primary" />
                    </div>
                </div>
                <div class="col-md-6">
                    <img id="Logo" alt="Logo-Preview" name="LogoImage" class="prev" style="width:192px;height:45px;" />
                </div>
            </div>
        </div>
    </div>
}

here,HttpPostedFileBase file retun always null when i am upload any file or image.


I try all the thing which i learn from research for MVC but did't work so how can i solve.at design side i am using model for get data and pass the data

[HttpPost]
        public ActionResult Logo(HttpPostedFileBase file, LogoViewModel model)
        {
            if (ModelState.IsValid)
            {
                if (Helper.IsValidImageFile(file.FileName))
                {
                    LogoMaster objlogo = new LogoMaster();
                    try
                    {
                        if (file != null)
                        {
                            string filename = Helper.ToValidFileName(Path.GetFileName(file.FileName));
                            string path = Path.Combine(Server.MapPath("~/Images/" + filename));
                            string filepathtosave = "~/Images/" + filename;
                            file.SaveAs(path);
                            objlogo.LogoImage = filepathtosave;
                        }
                        var IsExist = from i in _DBContext.LogoMasters select i;
                        if (IsExist.Count() > 0)
                        {
                            var Update = _DBContext.LogoMasters.First(x => x.LogoId == IsExist.FirstOrDefault().LogoId);
                            Update.LogoImage = objlogo.LogoImage;
                            _DBContext.SaveChanges();
                            return View();
                        }
                        else
                        {
                            objlogo.LogoImage = model.LogoImg;
                            if (model.logoid == null)
                                _DBContext.LogoMasters.Add(objlogo);
                            _DBContext.SaveChanges();
                            ModelState.Clear();
                        }
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
            }
            return View();

        }

Upvotes: 3

Views: 61

Answers (2)

Jasmin Patel
Jasmin Patel

Reputation: 188

This is very simple , i done one mistake that i saw you, just apply it than run it will work fine.

@using (Html.BeginForm("Logo", "Header", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <div class="col-md-12">
        <div class="panel">
            <div class="panel-heading">
                <h2>Logo</h2>
            </div>
            <div class="panel-body">
                <div class="col-md-6">
                    <div class="form-group">
                        <label>Logo Image<span style="color:red;">*</span>:</label>
                        @Html.HiddenFor(model => model.logoId)
                        @Html.TextBoxFor(model => model.LogoImage, new { @type = "file", @Id = "Files" })
                        @Html.ValidationMessageFor(model => model.LogoImage)
                    </div>
                    <span style="color:red;">Note:Please Upload Maximum Image size is 6 MB!!</span><br />
                    <span style="color:red;">Note:Allowed Image size Height: 45px, Width: 192px</span>
                    <div class="form-group">
                        <input type="submit" name="submit" value="Upload" class="btn btn-sm btn-primary" />
                    </div>
                </div>
                <div class="col-md-6">
                    <img id="Logo" alt="Logo-Preview" name="LogoImage" class="prev" style="width:192px;height:45px;" />
                </div>
            </div>
        </div>
    </div>
}

just replace name with HttpPostedFileBase file to HttpPostedFileBase LogoImage like

[HttpPost]
        public ActionResult Logo(HttpPostedFileBase LogoImage, LogoViewModel model)
        {
...
//your code
...

return View();

        }

Upvotes: 4

Diako Hasani
Diako Hasani

Reputation: 1494

in your view add enctype="multipart/form-data" like this sample

<form action="/Account/Register" method="post" enctype="multipart/form-data">

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

</form>

Upvotes: 1

Related Questions