1

I am trying to upload photo to server using mvc. And I actually done this. But It does not work this without refresh page. I don't want to lose my previous datas when refreshed it. How can I fix this problem ? Can anyone help me for this?

//VIEW

  @using (Html.BeginForm("create_conference", "Home", FormMethod.Post, new { enctype = "multipart/form-data", id = "allInputsFormValidation" }))
                                            {
                                                @Html.AntiForgeryToken()
                                                @* image upload start *@
                                                <div class="form-group">
                                                    <div class="input-group">
                                                        <span class="btn btn-default btn-wd btn-info btn-fill btn-file input-group-addon">
                                                            <i class="fa fa-image"></i> <input type="file" name="file" id="imgInp" accept="image/*">
                                                        </span>
                                                        <input type="text" class="form-control" readonly id="image-path" value="">
                                                    </div>
                                                </div>
                                                @* image upload end *@
                                                <div id="enter-room-name">
                                                    <div class="form-group">
                                                        <input type="text" placeholder="Enter Name of Room" class="form-control" required id="room-name" autocomplete="off" style="text-transform:uppercase">
                                                    </div>
                                                    <button type="submit" name="add-room-submit" class="btn btn-info btn-round  btn-wd">Save</button>
                                                </div>
                                            }

//CONTROLLER

[ValidateAntiForgeryToken]
        public ActionResult create_conference(HttpPostedFileBase file)
        {
            var path = "";
            if (file != null)
            {
                if (file.ContentLength > 0)
                {
                    //for checking uploaded file is image or not
                    if(Path.GetExtension(file.FileName).ToLower()==".jpg"
                        || Path.GetExtension(file.FileName).ToLower()==".png"
                        || Path.GetExtension(file.FileName).ToLower()==".gif"
                        || Path.GetExtension(file.FileName).ToLower()==".jpeg")
                    {
                        path = Path.Combine(Server.MapPath("~/assets/img/conference-img"), file.FileName);
                        file.SaveAs(path);
                        ViewBag.UploadSuccess = true;
                    }
                }
            }
              return View();
        }
Engr. Arda
  • 167
  • 1
  • 1
  • 10
  • 1
    User ajax to send the file. Refer this https://stackoverflow.com/questions/38703182/cannot-bind-form-data-and-uploaded-file-in-controller/38703844#38703844 – Shyju Jul 07 '18 at 21:06
  • Refer also [How to append whole set of model to formdata and obtain it in MVC](https://stackoverflow.com/questions/29293637/how-to-append-whole-set-of-model-to-formdata-and-obtain-it-in-mvc/29293681#29293681) –  Jul 07 '18 at 22:54

0 Answers0