JQuery的阿贾克斯形式与IE浏览器中的文件上传不工作 [英] JQuery Ajax Form with File Upload not working in IE

查看:208
本文介绍了JQuery的阿贾克斯形式与IE浏览器中的文件上传不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用jQuery Ajax的形式上传文件,在Chrome和Firefox的作​​品很好,但它在IE浏览器不工作。它弹出窗口一个窗口,告诉我要保存我想上传的文件。

I'm using Jquery Ajax Form to upload files, which works well in Chrome and Firefox, but it doesnt work in IE. It pop ups a window telling me to save the file that I'm trying to upload.

我的code的一些例子中,如果需要的话,是这里仪式:
HTML:

Some example of my code, if necessary, is rite here: 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:

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除了每一个浏览器。 IM可能不是唯一的一个,但我还没有找到一个答案在那里。

Again, it works in every browser except IE8. Im probably not the only one, but I havent found an answer out there.

推荐答案

我还没有一个textarea试过没,但如果我定义的内容类型的动作为text / html内恢复正常工作:

I haven't tried with a textarea yet, but it works fine if I define the content type to return inside the action as text/html:

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

这篇关于JQuery的阿贾克斯形式与IE浏览器中的文件上传不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆