【问题标题】:JQuery Ajax Form with File Upload not working in IE带有文件上传的 JQuery Ajax 表单在 IE 中不起作用
【发布时间】:2012-06-29 07:43:39
【问题描述】:

我使用 Jquery Ajax Form 上传文件,在 Chrome 和 Firefox 中运行良好,但在 IE 中不起作用。它会弹出一个窗口,告诉我保存我要上传的文件。

如有必要,我的代码的一些示例在这里是仪式: HTML:

<div class="addNewDocumentContent">
<form id="AddNewDocForm" action="@Url.Action("AddNewDocument", "BidForm")" enctype="multipart/form-data" method="post">
<div>
    <input name="File" type="file" style="width: 80%;" />
</div>
<div>
    <label>
        @Labels.Name</label>
    <input type="text" name="Name" style="width: 80%;" />
</div>
<div style="text-align: right;">
    <button type="button" name="Back" value="Back">
       @Buttons.GoBack
    </button>
    <button type="submit" name="Add" value="Back">
        @Buttons.Add
    </button>
</div>
</form>

JS:

//Document Ready=============================================================================
$(function () { 

    $('#AddNewDocForm').ajaxForm({
        type: 'POST',
        beforeSubmit: function () {
            return $("#AddNewDocForm").valid();
        },
        success: function (documents) {
            FillDocuments(documents);
            $('#dialogAddNewDocument').dialog('close');
        }
    });
});
//Validate====================================================================================
//Validation=====================================================================================
$(function () {
    $("#AddNewDocForm").validate({
        ignore: ":not(:visible)",
        rules: {
            File: "required",
            Name: "required"
        }
    });
});
//=========================================================================================

动作

[HttpPost]
    public JsonResult AddNewDocument(DocumentModel document)
    {
        if (ModelState.IsValid)
        {
            List<DocumentModel> documents = null;
            if (Session["Documents"] != null)
            {
                documents = (List<DocumentModel>)Session["Documents"];
                var doc = documents.OrderByDescending(x => x.Number).Take(1).FirstOrDefault();

                document.Number = doc != null ? doc.Number + 1 : 1;
                document.FileName = document.File != null ? document.File.FileName : document.FileName;
                documents.Add(document);
            }
            else
            {
                documents = new List<DocumentModel>();
                document.Number = 1;
                document.FileName = document.File != null ? document.File.FileName : document.FileName;
                documents.Add(document);
                Session["Documents"] = documents;
            }

            var displaydocs = documents.Select(x => new
            {
                Name = x.Name,
                Number = x.Number,
                File = x.File != null ? x.File.FileName : x.FileName,
                Route = x.Route != null ? x.Route : "#",
            });

            return Json(displaydocs, JsonRequestBehavior.AllowGet);
        }
        else
        {
            return null;
        }
    }

最后是模型:

  public class DocumentModel
{
    public int Number { get; set; }
    [Required]
    public string Name { get; set; }
    [Required]
    public HttpPostedFileBase File { get; set; }

    public string FileName { get; set; }
    public string Route { get; set; }
}

同样,它适用于除 IE8 之外的所有浏览器。我可能不是唯一一个,但我还没有找到答案。

【问题讨论】:

    标签: jquery asp.net-mvc-3 internet-explorer file-upload ajaxform


    【解决方案1】:

    这个问题has beenmany times。发帖前请先搜索。 documentation 明确表示:

    支持 XMLHttpRequest Level 2 的浏览器将能够 无缝上传文件,甚至在上传时获得进度更新 收益。对于较旧的浏览器,使用了一种后备技术,该技术 涉及 iframe,因为无法使用 XMLHttpRequest 对象的 1 级实现。这是一个常见的 回退技术,但它具有固有的局限性。内嵌框架 元素被用作表单提交操作的目标 表示将服务器响应写入 iframe。这可以 如果响应类型是 HTML 或 XML,但如果 响应类型是脚本或 JSON,这两者通常都包含 需要使用实体引用来表示的字符 在 HTML 标记中找到。

    在使用时考虑到脚本和 JSON 响应的挑战 在 iframe 模式下,表单插件允许嵌入这些响应 在 textarea 元素中,建议您这样做 与文件上传和旧版本结合使用时的响应类型 浏览器。但是请注意,如果在 表单然后请求使用普通 XHR 提交表单(不是 框架)。这给你的服务器代码增加了知道何时使用的负担 一个文本区域,什么时候不要。

    由于您要从控制器操作返回 JSON,因此您需要遵守文档中的说明 => 包装在 &lt;textarea&gt; 元素中。

    【讨论】:

      【解决方案2】:

      我还没有尝试过使用 textarea,但是如果我将内容类型定义为在操作中返回为 text/html,它就可以正常工作:

       return new JsonResult() { ContentType = "text/html", Data = result };
      

      【讨论】:

        【解决方案3】:

        尝试在您的 AJAX 调用中添加 cache:'false'... 类似的东西:

         $.ajax({
         type:"POST", 
         url:'process.php',
         cache:'false',   //IE FIX
         data: data, 
         success: function(){ //on success do something...
        
         } 
         });
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-09-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-03-08
          • 2013-03-03
          相关资源
          最近更新 更多