0

I want to upload image in my application without use of any JavaScript. I am new to .NET MVC development.

This is Edit View where I want to change the different image from local machine.

I am not sure whether it is possible or not. Anyone Please guide me here.

I have tried this code but file is not uploading.

@model XX.X.Models.File.ClsUpload

 @Html.HiddenFor(model => model.FilePath)
  <input type='file'/>
  <img src=@Model.FilePath alt="Image" />

  <input type="submit" value="Save" />
MVC
  • 649
  • 7
  • 23
  • Bootstrap has nothing to do with uploading, no matter the *backend*. – Panagiotis Kanavos Jul 09 '18 at 11:19
  • @PanagiotisKanavos, How can I upload the image then. Please suggest me. Thank you. – MVC Jul 09 '18 at 11:20
  • Possible duplicate of [MVC 4 Razor File Upload](https://stackoverflow.com/questions/15680629/mvc-4-razor-file-upload) – Panagiotis Kanavos Jul 09 '18 at 11:24
  • @PanagiotisKanavos, I am using `model` and `@Ajax.BeginForm`. Please have a look on my another question `https://stackoverflow.com/questions/51246819/how-to-upload-image-using-ajax-beginform-in-mvc`. Please provide some solutions. – MVC Jul 09 '18 at 13:51

1 Answers1

1

Using this code with .Net MVC i am saving employees pic you can try this code withOut javascript

View Code

@using (Html.BeginForm("Create", "Employees", FormMethod.Post, new { enctype = 
"multipart/form-data" }))
{
<div class="row">
 <div class="col-md-12">
   <div class="form-group">
    <input id="AttachmentName" name="AttachmentName" type="file">
   </div>
  </div>
</div>

}

Controller code

[HttpPost]
    public ActionResult Create(EmployeeClass employeeClass, HttpPostedFileBase AttachmentName)
    {

        string newProfileName = "";
        if (AttachmentName != null)
        {
            HttpPostedFileBase AttachmentNameFile = Request.Files["AttachmentName"];
            string productImageExt = Path.GetExtension(AttachmentNameFile.FileName);
            newProfileName = DateTime.Now.ToString("MMddyyHHmmss") + productImageExt;
            var saveimage = SaveImage(AttachmentNameFile, newProfileName);
        }
}


 public bool SaveImage(HttpPostedFileBase AttachmentNameFile, string FileName)
    {

            if (AttachmentNameFile.FileName != "")
            {

                string folderexist = Server.MapPath("~/images/ProfileImage/");
                bool isExist = System.IO.Directory.Exists(folderexist);
                if (!isExist)
                {
                    System.IO.Directory.CreateDirectory(folderexist);
                }
                AttachmentNameFile.SaveAs(folderexist + FileName);
            }
            return true;

    }