使用jQuery发送multipart/formdata.ajax在IE中

Send multipart/formdata with jQuery.ajax in IE

本文关键字:ajax IE formdata jQuery 发送 multipart 使用      更新时间:2023-09-26

是否有办法在Internet Explorer中实现以下解决方案?(IE7及以上版本)

链接:用jQuery.ajax发送multipart/formdata

解决方案代码在除IE以外的所有浏览器中都能很好地工作。

不可以,你不能使用jQuery.ajax上传文件,不幸的是,IE不支持FormData

查看Uploadify jQuery插件通过ajax上传文件。你也可以使用jQuery表单插件通过ajax上传文件

遗憾的是,IE还不支持FormData API。但是,您可以使用任意数量的jQuery AJAX表单post插件来实现类似的功能,例如这个。

我也遇到过这个问题,它可能对需要帮助的人有用。FormData仅在IE10以后才被支持,这里有一个链接。错误,因为你不能在旧的浏览器中绑定输入字段,就像在使用FormData的现代浏览器中。在IE中不能通过AJAX上传文件。有两种方法可以做

  • 使用一些插件,如Uploadify jQuery插件通过
    上传文件ajax。你也可以使用jQuery表单插件
    多和uploadify

下面是代码

  if(!isAjaxUploadSupported()){ //IE fallfack
            var iframe = document.createElement("<iframe name='upload_iframe_myFile' id='upload_iframe_myFile'>");
            iframe.setAttribute("width", "0");
            iframe.setAttribute("height", "0");
            iframe.setAttribute("border", "0");
            iframe.setAttribute("src","javascript:false;");
            iframe.style.display = "none";
            var form = document.createElement("form");
            form.setAttribute("target", "upload_iframe_myFile");
            form.setAttribute("action", "fileupload.aspx"); //change page to post
            form.setAttribute("method", "post");
            form.setAttribute("enctype", "multipart/form-data");
            form.setAttribute("encoding", "multipart/form-data");
            form.style.display = "block";
            var files = document.getElementById("myFile");//Upload Id
            form.appendChild(files);
            $conv("#container_myFile").html("Uploading...");
            document.body.appendChild(form);
            document.body.appendChild(iframe);
            iframeIdmyFile = document.getElementById("upload_iframe_myFile");
            // Add event...
            var eventHandlermyFile = function () {
                if (iframeIdmyFile.detachEvent) 
                    iframeIdmyFile.detachEvent("onload", eventHandlermyFile);
                else 
                    iframeIdmyFile.removeEventListener("load", eventHandlermyFile, false);
                response = getIframeContentJSON(iframeIdmyFile);
            }
            if (iframeIdmyFile.addEventListener) 
                iframeIdmyFile.addEventListener("load", eventHandlermyFile, true);
            if (iframeIdmyFile.attachEvent) 
                iframeIdmyFile.attachEvent("onload", eventHandlermyFile);
            form.submit();
            return;
        }
        ////var data = new FormData();
        //// code go here(for modern browsers)

        function isAjaxUploadSupported(){
                var input = document.createElement("input");
                input.type = "file";
                return (
                    "multiple" in input &&
                        typeof File != "undefined" &&
                        typeof FormData != "undefined" &&
                        typeof (new XMLHttpRequest()).upload != "undefined" );
        }
        function getIframeContentJSON(iframe){
                //IE may throw an "access is denied" error when attempting to access contentDocument on the iframe in some cases
                try {
                    // iframe.contentWindow.document - for IE<7
                    var doc = iframe.contentDocument ? iframe.contentDocument: iframe.contentWindow.document,
                        response;
                    var innerHTML = doc.body.innerHTML;
                    //plain text response may be wrapped in <pre> tag
                    if (innerHTML.slice(0, 5).toLowerCase() == "<pre>" && innerHTML.slice(-6).toLowerCase() == "</pre>") {
                        innerHTML = doc.body.firstChild.firstChild.nodeValue;
                    }
                    response = eval("(" + innerHTML + ")");
                } catch(err){
                    response = {success: false};
                }
                return response;
            }

尝试这样设置表单的属性:

$("#yourformid") .attr("enctype", "multipart/form-data") .attr("encoding", "multipart/form-data");

或者尝试找到现成的jquery上传插件