{"id":3364,"date":"2023-02-10T11:53:44","date_gmt":"2023-02-10T03:53:44","guid":{"rendered":"http:\/\/blog.hoyo.idv.tw\/?p=3364"},"modified":"2023-02-10T11:53:44","modified_gmt":"2023-02-10T03:53:44","slug":"xmlhttprequest-%e9%9d%9e%e5%90%8c%e6%ad%a5%e4%b8%8a%e5%82%b3%e6%aa%94%e6%a1%88%e5%8f%8a%e4%b8%8b%e8%bc%89","status":"publish","type":"post","link":"https:\/\/blog.hoyo.idv.tw\/?p=3364","title":{"rendered":"XMLHttpRequest - \u975e\u540c\u6b65\u4e0a\u50b3\u6a94\u6848\u53ca\u4e0b\u8f09"},"content":{"rendered":"<p>--<\/p>\n<h2>\u4e0a\u50b3<\/h2>\n<ul>\n<li><a href=\"https:\/\/developer.mozilla.org\/zh-CN\/docs\/Web\/Guide\/Using_FormData_Objects\" target=\"_blank\" rel=\"noopener\">FormData \u5bf9\u8c61\u7684\u4f7f\u7528<\/a><\/li>\n<\/ul>\n<pre class=\"lang:js decode:true\">var SelectForm = document.querySelector('#id_form_edit');\r\nvar fd = new FormData(SelectForm);\r\nvar xhr = new XMLHttpRequest();\r\n\r\nxhr.addEventListener(\"load\", function (j) {\r\n    const json = JSON.parse(j.target.responseText);\r\n    \/\/ json \u56de\u50b3\u5f8c\u8655\u7406\r\n}, false);\r\n\r\nxhr.open(\"POST\", '\/cs\/v2\/Question_concept\/update_concept', true);\r\nxhr.send(fd);\r\n<\/pre>\n<p>--<\/p>\n<h2>\u4e0b\u8f09<\/h2>\n<p>\u4f7f\u7528 ajax \u4f86\u4e0b\u8f09\u7684\u597d\u8655\u662f\uff0c\u5c0d\u65bc PHP \u5c31\u4e0d\u6703\u6709\u8a18\u61b6\u9ad4\u7684\u58d3\u529b\u3002\u4ee5 PHP \u8f38\u51fa Excel \u70ba\u4f8b\uff0c\u5927\u8cc7\u6599\u6642\u6703\u9047\u5230 <code>Fatal error: Allowed memory size of<\/code> \u9019\u985e\u7684\u932f\u8aa4\uff0c\u5c07\u7a0b\u5f0f\u6539\u6210\u53ea\u8f38\u51fa\u4e0d\u7522\u751f\u6a94\u6848\uff0c\u8b93\u524d\u7aef\u4f86\u7d44\u6210\u6a94\u6848\u5c31\u4e0d\u6703\u6709\u9019\u500b\u9650\u5236\uff0c\u4e5f\u5c31\u662f\u5c07<\/p>\n<pre class=\"lang:php decode:true \">$objWriter-&gt;save(\"hoyo.xls\");<\/pre>\n<p>\u6539\u6210<\/p>\n<pre class=\"lang:php decode:true \">$objWriter-&gt;save('php:\/\/output');<\/pre>\n<p>\u7136\u5f8c\u5c31\u53ef\u4ee5\u4f7f\u7528\u4e0b\u9762\u7684\u65b9\u6cd5\u5b58\u6210\u6a94\u6848<\/p>\n<ul>\n<li><a class=\"question-hyperlink\" href=\"https:\/\/stackoverflow.com\/questions\/22724070\/prompt-file-download-with-xmlhttprequest\" target=\"_blank\" rel=\"noopener\">Prompt file download with XMLHttpRequest<\/a><\/li>\n<\/ul>\n<pre class=\"lang:js decode:true\">function saveBlob(blob, fileName) {\r\n    let a = document.createElement('a');\r\n    a.href = window.URL.createObjectURL(blob);\r\n    a.download = fileName;\r\n    a.dispatchEvent(new MouseEvent('click'));\r\n}\r\n\r\nconst SelectForm = document.querySelector('#id_form_question_check');\r\nconst fd = new FormData(SelectForm);\r\nconst xhr = new XMLHttpRequest();\r\nxhr.responseType = 'blob';\r\nxhr.open(\"POST\", '\/cs\/v2\/download_excel', true);\r\n\r\nxhr.onload = function (e) {\r\n    $.LoadingOverlay(\"hide\");\r\n    var blob = e.currentTarget.response;\r\n    var contentDispo = e.currentTarget.getResponseHeader('Content-Disposition');\r\n    saveBlob(blob, '\u8a66\u984c\u6aa2\u67e5_'+ date('YmdHis') +'.xls');\r\n};\r\n\r\nxhr.send(fd);\r\n<\/pre>\n<p>--<\/p>\n<h2>\u591a\u6a94\u6848\u4e0b\u8f09<\/h2>\n<p>\u672c\u4f86\u662f\u8981\u5c07\u6a94\u6848\u5e36\u5728 JSON \u9663\u5217\u5167\uff0c\u5c07\u4e8c\u9032\u5236 encode Base64\uff0c\u5230\u524d\u7aef\u518d decode \uff0c\u7d50\u679c JavaScript decode \u4e8c\u9032\u5236 Base64 \u6709\u554f\u984c\uff0c\u7121\u6cd5 100% \u9084\u539f\uff0c\u6240\u4ee5\u5c31\u4f7f\u7528\u524d\u7aef\u8ff4\u5708\u7684\u65b9\u5f0f\u4f86\u4e0b\u8f09<\/p>\n<p>\u9019\u500b\u7bc4\u4f8b\u548c\u524d\u9762\u4e0d\u540c\u7684\u662f\u591a\u4e86\u4e00\u500b\u6aa2\u67e5\u4e0b\u8f09\u6a94\u6848\u5927\u5c0f\u662f\u5426\u5927\u65bc 0<\/p>\n<pre class=\"lang:js decode:true\">function saveBlob(blob, fileName) {\r\n    let a = document.createElement('a');\r\n    a.href = window.URL.createObjectURL(blob);\r\n    a.download = fileName;\r\n    a.dispatchEvent(new MouseEvent('click'));\r\n}\r\n\r\nfunction download_word(){\r\n    let $q = explode(',', $('#id_form').find('[name=\"subject\"]').val());\r\n\r\n    $.each( $q, function(k,v){\r\n        const xhr = new XMLHttpRequest();\r\n        xhr.responseType = 'blob';\r\n        xhr.open(\"POST\", '\/download_word\/' + v, true);\r\n\r\n        xhr.onload = function (e) {\r\n            let blob = e.currentTarget.response;\r\n            console.log(blob.size);\r\n            if ( blob.size &gt; 0 ) saveBlob(blob, 'html_'+ v + '.doc');\r\n        };\r\n        xhr.send();\r\n    } );\r\n}<\/pre>\n<p>--<\/p>\n<h2>new FormData \u9664\u4e86\u539f\u8868\u55ae\u9084\u8981\u9644\u52a0\u8cc7\u6599<\/h2>\n<ul>\n<li><a href=\"https:\/\/developer.mozilla.org\/zh-CN\/docs\/Web\/API\/FormData\/append\" target=\"_blank\" rel=\"noopener\">FormData.append()<\/a><\/li>\n<\/ul>\n<pre class=\"lang:default decode:true\">function SubmitComplete(){\r\n    \/\/ ....\r\n}\r\n\r\nvar fd = new FormData(SelectForm);\r\nfd.append('Name', 'ABC 999');\r\n\r\nvar xhr = new XMLHttpRequest();\r\nxhr.addEventListener(\"load\", SubmitComplete, false);\r\nxhr.open(\"POST\", \"?a=Admin\/Supervisor&amp;b=Update&amp;t=\" + Math.random(),true);\r\nxhr.send(fd);<\/pre>\n<p>--<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p class=\"pvc_stats all \" data-element-id=\"3364\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> &nbsp;1,319&nbsp;total views<\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>-- \u4e0a\u50b3 FormData ...<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p class=\"pvc_stats all \" data-element-id=\"3364\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> &nbsp;1,319&nbsp;total views<\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[31],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/3364"}],"collection":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3364"}],"version-history":[{"count":3,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/3364\/revisions"}],"predecessor-version":[{"id":11533,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/3364\/revisions\/11533"}],"wp:attachment":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3364"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3364"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3364"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}