{"id":769,"date":"2015-11-01T19:07:54","date_gmt":"2015-11-01T11:07:54","guid":{"rendered":"http:\/\/www.zyuns.com\/?p=769"},"modified":"2015-11-01T19:07:54","modified_gmt":"2015-11-01T11:07:54","slug":"%e4%bf%ae%e5%a4%8d%e7%89%88ajaxfileuploader","status":"publish","type":"post","link":"https:\/\/www.siediyer.cn\/?p=769","title":{"rendered":"\u4fee\u590d\u7248AjaxFileUploader"},"content":{"rendered":"<p>AjaxFileUploaderV2.1\u4fee\u6539\u7248\u4e3b\u8981\u4fee\u590d\u4ee5\u4e0bBUG:<br \/>\n1.\u5bf9\u4e8e\u66f4\u9ad8\u7248\u672cjquery\u6ca1\u6709handleError\u51fd\u6570\u7684\u51fa\u9519\u5904\u7406.<br \/>\n2.\u5bf9\u4e8e\u670d\u52a1\u5668\u54cd\u5e94\u7684ContentType\u4e0d\u540c\u8bbe\u7f6e\u53ef\u80fd\u4f1a\u5305\u542b\u591a\u4f59\u7684pre\u6807\u7b7e\u51fa\u9519\u5904\u7406.<br \/>\n3.\u5bf9\u4e8e\u53d1\u8bf7\u6c42\u7684dataType\u8bbe\u4e3ahtml\u51fa\u9519\u7684\u5904\u7406.<br \/>\n4.\u5bf9\u4e8etextarea\u5b57\u6bb5(\u503c\u542b\u6709\u53cc\u5f15\u53f7\u7b49\u7279\u6b8a\u5b57\u7b26\u53f7\u65f6,\u4f20\u5230\u5230\u670d\u52a1\u5668\u53d1\u73b0\u4f1a\u503c\u88ab\u622a\u53d6)\u51fa\u9519\u7684\u5904\u7406.<\/p>\n<pre class=\"lang:default decode:true \">jQuery.extend({\r\n    createUploadIframe: function (id, uri) {\r\n        \/\/create frame\r\n        var frameId = 'jUploadFrame' + id;\r\n        var iframeHtml = '&lt;iframe id=\"' + frameId + '\" name=\"' + frameId + '\" style=\"position:absolute; top:-9999px; left:-9999px\"';\r\n        if (window.ActiveXObject) {\r\n            if (typeof uri == 'boolean') {\r\n                iframeHtml += ' src=\"'%20+%20'javascript:false'%20+%20'\"';\r\n            }\r\n            else if (typeof uri == 'string') {\r\n                iframeHtml += ' src=\"'%20+%20uri%20+%20'\"';\r\n            }\r\n        }\r\n        iframeHtml += ' \/&gt;';\r\n        jQuery(iframeHtml).appendTo(document.body);\r\n        return jQuery('#' + frameId).get(0);\r\n    },\r\n    createUploadForm: function (id, fileElementId, data, textareas) {\r\n        \/\/create form\r\n        var formId = 'jUploadForm' + id;\r\n        var fileId = 'jUploadFile' + id;\r\n        var form = jQuery('&lt;form  action=\"\" method=\"POST\" name=\"' + formId + '\" id=\"' + formId + '\" enctype=\"multipart\/form-data\"&gt;&lt;\/form&gt;');\r\n        if (data) {\r\n            for (var i in data) {\r\n                jQuery('&lt;input type=\"hidden\" name=\"' + i + '\" value=\"' + data[i] + '\" \/&gt;').appendTo(form);\r\n            }\r\n        }\r\n        if (textareas) {\r\n            for (var i in textareas) {\r\n                jQuery('&lt;textarea name=\"' + i + '\"&gt;'+textareas[i]+'&lt;\/textarea&gt;').appendTo(form);\r\n            }\r\n        }\r\n        var oldElement = jQuery('#' + fileElementId);\r\n        var newElement = jQuery(oldElement).clone();\r\n        jQuery(oldElement).attr('id', fileId);\r\n        jQuery(oldElement).before(newElement);\r\n        jQuery(oldElement).appendTo(form);\r\n        \/\/set attributes\r\n        jQuery(form).css('position', 'absolute');\r\n        jQuery(form).css('top', '-1200px');\r\n        jQuery(form).css('left', '-1200px');\r\n        jQuery(form).appendTo('body');\r\n        return form;\r\n    },\r\n\r\n    ajaxFileUpload: function (s) {\r\n        \/\/ TODO introduce global settings, allowing the client to modify them for all requests, not only timeout\t\t\r\n        s = jQuery.extend({}, jQuery.ajaxSettings, s);\r\n        var id = new Date().getTime()\r\n        var form = jQuery.createUploadForm(id, s.fileElementId, (typeof(s.data) == 'undefined' ? false : s.data),(typeof(s.textareas) == 'undefined' ? false : s.textareas));\r\n        var io = jQuery.createUploadIframe(id, s.secureuri);\r\n        var frameId = 'jUploadFrame' + id;\r\n        var formId = 'jUploadForm' + id;\r\n        \/\/ Watch for a new set of requests\r\n        if (s.global &amp;&amp; !jQuery.active++) {\r\n            jQuery.event.trigger(\"ajaxStart\");\r\n        }\r\n        var requestDone = false;\r\n        \/\/ Create the request object\r\n        var xml = {}\r\n        if (s.global)\r\n            jQuery.event.trigger(\"ajaxSend\", [xml, s]);\r\n        \/\/ Wait for a response to come back\r\n        var uploadCallback = function (isTimeout) {\r\n            var io = document.getElementById(frameId);\r\n            try {\r\n                if (io.contentWindow) {\r\n                    xml.responseText = io.contentWindow.document.body ? jQuery('&lt;div&gt;').html(io.contentWindow.document.body.innerHTML).text(): null;\r\n                    xml.responseXML = io.contentWindow.document.XMLDocument ? io.contentWindow.document.XMLDocument : io.contentWindow.document;\r\n                } else if (io.contentDocument) {\r\n                    xml.responseText = io.contentWindow.document.body ? jQuery('&lt;div&gt;').html(io.contentWindow.document.body.innerHTML).text(): null;\r\n                    xml.responseXML = io.contentDocument.document.XMLDocument ? io.contentDocument.document.XMLDocument : io.contentDocument.document;\r\n                }\r\n            } catch (e) {\r\n                jQuery.handleError(s, xml, null, e);\r\n            }\r\n            if (xml || isTimeout == \"timeout\") {\r\n                requestDone = true;\r\n                var status;\r\n                try {\r\n                    status = isTimeout != \"timeout\" ? \"success\" : \"error\";\r\n                    \/\/ Make sure that the request was successful or notmodified\r\n                    if (status != \"error\") {\r\n                        \/\/ process the data (runs the xml through httpData regardless of callback)\r\n                        var data = jQuery.uploadHttpData(xml, s.dataType);\r\n                        \/\/ If a local callback was specified, fire it and pass it the data\r\n                        if (s.success)\r\n                            s.success(data, status);\r\n                        \/\/ Fire the global callback\r\n                        if (s.global)\r\n                            jQuery.event.trigger(\"ajaxSuccess\", [xml, s]);\r\n                    } else\r\n                        jQuery.handleError(s, xml, status);\r\n                } catch (e) {\r\n                    status = \"error\";\r\n                    jQuery.handleError(s, xml, status, e);\r\n                }\r\n                \/\/ The request was completed\r\n                if (s.global)\r\n                    jQuery.event.trigger(\"ajaxComplete\", [xml, s]);\r\n                \/\/ Handle the global AJAX counter\r\n                if (s.global &amp;&amp; !--jQuery.active)\r\n                    jQuery.event.trigger(\"ajaxStop\");\r\n                \/\/ Process result\r\n                if (s.complete)\r\n                    s.complete(xml, status);\r\n                jQuery(io).unbind()\r\n                setTimeout(function () {\r\n                    try {\r\n                        jQuery(io).remove();\r\n                        jQuery(form).remove();\r\n                    } catch (e) {\r\n                        jQuery.handleError(s, xml, null, e);\r\n                    }\r\n                }, 100)\r\n                xml = null\r\n            }\r\n        }\r\n        \/\/ Timeout checker\r\n        if (s.timeout &gt; 0) {\r\n            setTimeout(function () {\r\n                \/\/ Check to see if the request is still happening\r\n                if (!requestDone) uploadCallback(\"timeout\");\r\n            }, s.timeout);\r\n        }\r\n        try {\r\n            var form = jQuery('#' + formId);\r\n            jQuery(form).attr('action', s.url);\r\n            jQuery(form).attr('method', 'POST');\r\n            jQuery(form).attr('target', frameId);\r\n            if (form.encoding) {\r\n                jQuery(form).attr('encoding', 'multipart\/form-data');\r\n            }\r\n            else {\r\n                jQuery(form).attr('enctype', 'multipart\/form-data');\r\n            }\r\n            jQuery(form).submit();\r\n\r\n        } catch (e) {\r\n            jQuery.handleError(s, xml, null, e);\r\n        }\r\n        jQuery('#' + frameId).load(uploadCallback);\r\n        return {abort: function () {\r\n        }};\r\n\r\n    },\r\n    uploadHttpData: function (r, type) {\r\n        var data = !type;\r\n        data = type == \"xml\" || data ? r.responseXML : r.responseText;\r\n        \/\/ If the type is \"script\", eval it in global context\r\n        if (type == \"script\")\r\n            jQuery.globalEval(data);\r\n        \/\/ Get the JavaScript object, if JSON is used.\r\n        if (type == \"json\")\r\n            eval(\"data = \" + data);\r\n        \/\/ evaluate scripts within html\r\n        if (type == \"html\")\r\n            jQuery(\"&lt;div&gt;\").html(data).find('script').remove();\r\n        return data;\r\n    },\r\n    handleError: function( s, xml, status, e ) {\r\n        \/\/ If a local callback was specified, fire it\r\n        if ( s.error )\r\n            s.error( xml, status, e );\r\n        \/\/ Fire the global callback\r\n        if ( s.global )\r\n            jQuery.event.trigger( \"ajaxError\", [xml, s, e] );\r\n    }\r\n})\r\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>AjaxFileUploaderV2.1\u4fee\u6539\u7248\u4e3b\u8981\u4fee\u590d\u4ee5\u4e0bBUG: 1.\u5bf9\u4e8e\u66f4\u9ad8\u7248\u672cjquery\u6ca1\u6709handl [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-769","post","type-post","status-publish","format-standard","hentry","category-javascript"],"_links":{"self":[{"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/posts\/769","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=769"}],"version-history":[{"count":1,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/posts\/769\/revisions"}],"predecessor-version":[{"id":770,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/posts\/769\/revisions\/770"}],"wp:attachment":[{"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=769"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=769"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=769"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}