如何解决从.net MVC Bootstrap Modal页面上的文件上传在IE中有效,但在Edge或Chrome或Firefox中不起作用
我正在编写一个.net MVC 4.7 Web应用程序,其中大多数工作正常。一页上有一系列选项卡,每个选项卡代表一种实体类型,其中一个是图像。您可以创建,编辑,删除代表该页面所针对的提供程序图片的文件。当您执行创建/编辑/删除操作时,将弹出一个模式窗口,您可以执行已调用的操作。创建图像时会出现我的问题,弹出模式对话框,选择图像类型,选择文件并添加注释,然后按Enter键在数据库中创建记录。当我使用IE浏览器(v11)时,它可以正常工作,但任何其他浏览器(Edge,Chrome和Firefox)都无法正常工作,尽管ProviderId值位于Id中,但Create方法中的image Object大多为空值。图像对象的字段。我不知道这是怎么回事。使用Bootstrap 3.3.7和Jquery 3.4.1
public ActionResult Create(int id)
{
Image image = new Image();
image.ProviderId = id;
//ViewBag.ProviderId = id;
ViewBag.ImageTypeId = DataHelper.GetImageTypeSelectList(db,(String)Session["BusUnit"],null);
//return PartialView("Create");
return PartialView("Create",image);
}
// POST: Images/Create
// To protect from overposting attacks,please enable the specific properties you want to bind to,for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
/// <summary>
///
/// </summary>
/// <param name="image"></param>
/// <param name="File"></param>
/// <returns></returns>
[HttpPost]
public ActionResult Create(Image image,HttpPostedFileBase File)
{
if (ModelState.IsValid)
{
if (image.File.HasFile())
{
if (!image.File.IsSizeValid())
{
ModelState.AddModelError("File","File size must be less than 6 MB");
}
else
{
try
{
image.FileName = Path.GetFileName(image.File.FileName);
image.FileSize = image.File.ContentLength;
image.FileSystemType = image.File.ContentType;
image.FileContent = new byte[image.FileSize];
image.File.InputStream.Read(image.FileContent,image.FileSize);
image.UploadDate = System.DateTime.Now;
// if (image.File.IsImageFile())
// {
// System.Drawing.Image img = System.Drawing.Image.FromStream(image.File.InputStream);
// int height = img.Height;
// int width = img.Width;
// }
image.CreatedBy = User.Identity.Name.Replace("1UPMC-ACCT\\","");
image.CreatedDate = System.DateTime.Now;
db.Images.Add(image);
db.SaveChanges();
string url = Url.Action("Index","Images",new { id = image.ProviderId });
return Json(new { success = true,url = url });
}
catch (DbEntityValidationException e)
{
StringBuilder message = DataHelper.GetAllEntityValidationErrorMsgs(e);
logger.ErrorFormat("Database validation Exception -> {0}",message,e);
ModelState.AddModelError(String.Empty,message.ToString());
}
catch (DbUpdateException s)
{
if (s.InnerException != null)
{
var sqlException = s.InnerException as System.Data.Entity.Core.UpdateException;
if (sqlException.InnerException != null)
{
var innerException = sqlException.InnerException as System.Data.SqlClient.SqlException;
if (innerException.Number == 2601 || innerException.Number == 2627)
{
ModelState.AddModelError("File","Duplicate File,you can not add this File for this Provider again.");
}
else
{
String messages = DataHelper.GetAllExceptionMessages(s);
logger.ErrorFormat("Exception thrown {0}",messages,s);
ModelState.AddModelError(String.Empty,messages);
}
}
}
}
catch (System.Exception ex)
{
String messages = DataHelper.GetAllExceptionMessages(ex);
logger.ErrorFormat("Exception thrown {0}",ex);
ModelState.AddModelError(String.Empty,messages);
} // end of try catch block
} // end of normal processing
}
else
{
ModelState.AddModelError("File","Select a File to upload.");
} // end of hasfile
}
else
{
var errors = ModelState.Where(m => m.Value.Errors.Count() > 0);
foreach (var kvp in errors.ToList())
{
ModelState.AddModelError(string.Empty,string.Format("{0}:{1}",kvp.Key,kvp.Value.Errors[0].ErrorMessage));
}
} // end of is valid
ViewBag.ProviderId = image.ProviderId;
ViewBag.ImageTypeId = DataHelper.GetImageTypeSelectList(db,image.ImageTypeId);
return PartialView("Create",image);
}
这是cshtml:
@model ProviderTracker.Models.Image
@using ProviderTracker.Models
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-
hidden="true">×</button>
<h4 class="modal-title" id="myModalLabel">Create an Image</h4>
</div>
@using (Html.BeginForm("Create",FormMethod.Post,new { enctype ="multipart/form-data" }))
{
<div class="modal-body">
@*@Html.AntiForgeryToken()*@
<div class="form-horizontal">
@Html.ValidationSummary(true,"",new { @class = "text-danger" })
@Html.HiddenFor(model => model.ProviderId)
<div class="form-group">
@Html.LabelFor(model => model.ImageTypeId,"Image Type",htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("ImageTypeId",null,"Select...",htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.ImageTypeId,new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.File,"Select File :",htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(model => model.File,new { @type = "file",@id="File",@class = "form-control" })
@*@Html.FileFor(model => model.File,new { @class = "form-control" })*@
@Html.ValidationMessageFor(model => model.File,new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Comment,htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Comment,new { htmlAttributes = new { @class = "form-control",id = "Comment" } })
@Html.ValidationMessageFor(model => model.Comment,new { @class = "text-danger" })
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button class="btn" type="button" data-dismiss="modal">Return to List</button>
<input class="btn btn-primary" type="submit" value="Save" />
</div>
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。