从.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">&times;</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 (将#修改为@)