This is my View :
@model SimpleUpload.Models.MemberModel@{ViewBag.Title = "ContactForm";}ContactForm
@using (Html.BeginForm("ContactForm", "Home", FormMethod.Post, new { enctype = "multipart/form-data" })){@Html.AntiForgeryToken()MemberModel
@Html.LabelFor(model => model.ImageFile, htmlAttributes: new { @class = "control-label col-md-2" })@Html.TextBoxFor(m => m.ImageFile, new { type = "file" })}
I want to upload the file posted in the form group div.
This my Controller method:
[HttpGet]
public ActionResult ContactForm(){return View();}
[HttpPost]
public ActionResult ContactForm(MemberModel membervalues)
{//Use Namespace called : System.IOstring FileName = Path.GetFileNameWithoutExtension(membervalues.ImageFile.FileName);//To Get File Extensionstring FileExtension = Path.GetExtension(membervalues.ImageFile.FileName);//Add Current Date To Attached File NameFileName = DateTime.Now.ToString("yyyyMMdd") + "-" + FileName.Trim() + FileExtension;//Get Upload path from Web.Config file AppSettings.string UploadPath = ConfigurationManager.AppSettings["UserImagePath"].ToString();//Its Create complete path to store in server.membervalues.ImagePath = UploadPath + FileName;//To copy and save file into server.membervalues.ImageFile.SaveAs(membervalues.ImagePath);return View();
}
Now, whenever I click on the submit button I find this error 'Object reference not set to an instance of an object.' on the Filename. I cannot figure out where the problem is!
Model class :
public class MemberModel
{
[DisplayName("Upload File")]public string ImagePath { get; set; }public HttpPostedFile ImageFile { get; set; }
}
How can I Solve this?
Salman BegPosted Jan 23, 2019, 2:43 AM
Faisal PathanPosted Jan 23, 2019, 2:36 AM