Javascript 如何在不刷新页面的情况下使用mvc将照片上传到服务器?
我正在尝试使用mvc将照片上传到服务器。我真的做到了。但如果没有刷新页面,它将无法执行此操作。我不想在刷新之前的数据时丢失它。我如何解决这个问题?有人能帮我吗 //看法Javascript 如何在不刷新页面的情况下使用mvc将照片上传到服务器?,javascript,html,asp.net-mvc,html.beginform,Javascript,Html,Asp.net Mvc,Html.beginform,我正在尝试使用mvc将照片上传到服务器。我真的做到了。但如果没有刷新页面,它将无法执行此操作。我不想在刷新之前的数据时丢失它。我如何解决这个问题?有人能帮我吗 //看法 @using (Html.BeginForm("create_conference", "Home", FormMethod.Post, new { enctype = "multipart/form-data", id = "allInputsFormValidation" }))
@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>
}
使用ajax发送文件。请参阅本手册
[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();
}