最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Send multipartformdata with jQuery.ajax in IE - Stack Overflow

programmeradmin2浏览0评论

Is there a way to do the following solution in Internet Explorer? (IE7 and up)

link: Sending multipart/formdata with jQuery.ajax

The solution code works great in every browser but IE.

Is there a way to do the following solution in Internet Explorer? (IE7 and up)

link: Sending multipart/formdata with jQuery.ajax

The solution code works great in every browser but IE.

Share Improve this question edited May 23, 2017 at 12:31 CommunityBot 11 silver badge asked Oct 26, 2011 at 6:53 James NineJames Nine 2,61811 gold badges38 silver badges54 bronze badges 1
  • use this for multipart/format data freshdesignweb.com/wp-content/uploads/downloads/2011/01/… – Nilesh patel Commented Oct 19, 2012 at 7:05
Add a comment  | 

4 Answers 4

Reset to default 9

No, you can't use jQuery.ajax to upload files and FormData isn't supported by IE unfortunately.

Check out the Uploadify jQuery plugin to upload files via ajax. You can also use jQuery Form Plugin to upload files via ajax.

Unfortunately, IE doesn't support the FormData API yet. However, you can achieve something similar using any number of jQuery AJAX form post plugins, such as this one.

I too have faced this problem ,it may be useful to some one in need . FormData is supported only from IE10 onwards here's a link.Error because you cannot bind input fields in old browsers, like in a modern ones using FormData. You cannot upload files through AJAX in IE.Their are two alternative ways to do

  • Use some plugin like Uploadify jQuery plugin to upload files via
    ajax. You can also use jQuery Form Plugin to
    multi and uploadify

  • Other way is you may use iframe.

Here's is the code

  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;
            }

Try to set forms' attributes like this:

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

or rather try to find ready-made jquery upload plugin

发布评论

评论列表(0)

  1. 暂无评论