如何在没有刷新页面的情况下使用mvc将照片上传到服务器?

时间:2018-07-07 21:04:30

标签: javascript html asp.net-mvc html.beginform

我正在尝试使用mvc将照片上传到服务器。我实际上是这样做的。但是,如果没有刷新页面,它将无法正常工作。刷新时,我不想丢失以前的数据。我该如何解决这个问题?有人可以帮我吗?

// 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();
        }

0 个答案:

没有答案