{"id":10008002,"date":"2023-06-27T14:59:03","date_gmt":"2023-06-27T06:59:03","guid":{"rendered":"https:\/\/ityet.com\/?p=10008002"},"modified":"2023-06-27T14:59:03","modified_gmt":"2023-06-27T06:59:03","slug":"fetch%e5%bc%82%e6%ad%a5%e4%b8%8a%e4%bc%a0%e5%9b%be%e7%89%87%ef%bc%88%e9%99%84htmljavascriptphp%e4%bb%a3%e7%a0%81%ef%bc%89","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10008002","title":{"rendered":"fetch\u5f02\u6b65\u4e0a\u4f20\u56fe\u7247\uff08\u9644html+JavaScript+php\u4ee3\u7801\uff09"},"content":{"rendered":"<h2>\u6548\u679c<\/h2>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc8xdP\" alt=\"image.png\" title=\"image.png\" loading=\"lazy\"><\/p>\n<h2>index.html<\/h2>\n<pre><code>&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n    &lt;head&gt;\n        &lt;title&gt;\u56fe\u7247\u4e0a\u4f20\u793a\u4f8b&lt;\/title&gt;\n        &lt;meta charset=&quot;utf-8&quot;&gt;\n        &lt;script src=&quot;upload.js&quot;&gt;&lt;\/script&gt;\n        &lt;style&gt;\n            *{\n                padding: 0;\n                margin: 0;\n            }\n            #app{\n                width: 500px;\n                margin: 100px auto 0;\n                padding: 20px 20px;\n                background: #eee;\n                border-radius: 15px;\n            }\n        &lt;\/style&gt;\n    &lt;\/head&gt;\n&lt;body&gt;\n    \n    &lt;div id=&quot;app&quot;&gt;\n        &lt;h1&gt;fetch\u56fe\u7247\u4e0a\u4f20\u793a\u4f8b&lt;\/h1&gt;\n        \n        &lt;!--\u4e0a\u4f20\u8868\u5355--&gt;\n        &lt;input type=&quot;file&quot; id=&quot;imageFile&quot; accept=&quot;image\/*&quot;&gt;\n        \n        &lt;!--\u4e0a\u4f20\u7ed3\u679c--&gt;\n        &lt;div id=&quot;result&quot;&gt;&lt;\/div&gt;\n    &lt;\/div&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n<h2>upload.js<\/h2>\n<pre><code>\/\/ \u786e\u4fddJavaScript\u4ee3\u7801\u5728DOM\u52a0\u8f7d\u5b8c\u6210\u540e\u6267\u884c\ndocument.addEventListener('DOMContentLoaded', function() {\n    \n    \/\/ \u83b7\u53d6\u70b9\u51fb\u4e0a\u4f20\u7684\u6309\u94ae\n    var fileButton = document.getElementById('imageFile');\n    \n    \/\/ \u76d1\u542c\u9009\u62e9\u6587\u4ef6\u6309\u94ae\u662f\u5426\u5df2\u7ecf\u9009\u62e9\u4e86\u6587\u4ef6\n    fileButton.addEventListener('change', function (){\n        \n        \/\/ \u83b7\u53d6\u9009\u62e9\u7684\u6587\u4ef6\n        var fileSelected = fileButton.files[0];\n        \n        \/\/ \u6267\u884c\u4e0a\u4f20\u51fd\u6570\n        uploadFile(fileSelected, function(error, response) {\n            if (error) {\n                \n                \/\/ \u4e0a\u4f20\u6587\u4ef6\u5931\u8d25\n                console.log(error);\n            } else {\n                \n                \/\/ \u4e0a\u4f20\u6587\u4ef6\u6210\u529f\n                var jsonData = JSON.parse(response);\n                \n                console.log(jsonData);\n                \n                \/\/ \u663e\u793a\u4e0a\u4f20\u7ed3\u679c\u9884\u89c8\n                document.getElementById('result').innerHTML = '&lt;img src=&quot;'+jsonData.url+'&quot; width=&quot;350&quot; \/&gt;';\n            }\n        });\n    });\n    \n    \/\/ \u6e05\u7a7afile\u8868\u5355\u7684\u9009\u62e9\n    fileButton.value = '';\n})\n\n\/\/ \u4e0a\u4f20\u51fd\u6570\nfunction uploadFile(file, callback) {\n    \n    \/\/ \u83b7\u53d6\u8868\u5355\u6570\u636e\n    var formData = new FormData();\n    formData.append('file', file);\n    \n    \/\/ \u8bf7\u6c42\u4e0a\u4f20\u670d\u52a1\u5668\n    fetch('upload.php', {\n        method: 'POST',\n        body: formData,\n    })\n    .then(function(response) {\n        return response.text();\n    })\n    .then(function(data) {\n        callback(null, data);\n    })\n    .catch(function(error) {\n        callback(error, null);\n    });\n}<\/code><\/pre>\n<h2>upload.php<\/h2>\n<pre><code>&lt;?php\n\/\/ \u7f16\u7801\nheader(&quot;Content-type:application\/json&quot;);\n \n\/\/ \u83b7\u53d6\u6587\u4ef6\n$file = $_FILES[&quot;file&quot;][&quot;name&quot;];\n \n\/\/ \u83b7\u53d6\u6587\u4ef6\u540e\u7f00\u540d\n$hzm = substr($file,strpos($file,&quot;.&quot;));\n \n\/\/ \u8bbe\u7f6e\u65b0\u6587\u4ef6\u540d\n$newfile = date(&quot;Y-m-d&quot;).&quot;-&quot;.rand(100,999);\n \n\/\/ \u5141\u8bb8\u4e0a\u4f20\u7684\u540e\u7f00\n$allowedExts = array(&quot;gif&quot;, &quot;jpeg&quot;, &quot;jpg&quot;, &quot;png&quot;);\n$temp = explode(&quot;.&quot;, $file);\n$extension = end($temp);\n\nif ((($_FILES[&quot;file&quot;][&quot;type&quot;] == &quot;image\/gif&quot;)\n|| ($_FILES[&quot;file&quot;][&quot;type&quot;] == &quot;image\/jpeg&quot;)\n|| ($_FILES[&quot;file&quot;][&quot;type&quot;] == &quot;image\/jpg&quot;)\n|| ($_FILES[&quot;file&quot;][&quot;type&quot;] == &quot;image\/pjpeg&quot;)\n|| ($_FILES[&quot;file&quot;][&quot;type&quot;] == &quot;image\/x-png&quot;)\n|| ($_FILES[&quot;file&quot;][&quot;type&quot;] == &quot;image\/png&quot;))\n&amp;&amp; ($_FILES[&quot;file&quot;][&quot;size&quot;] &lt; 10485760)\n&amp;&amp; in_array($extension, $allowedExts)){\n    \n    \/\/ \u5224\u65ad\u4e0a\u4f20\u7ed3\u679c\n    if ($_FILES[&quot;file&quot;][&quot;error&quot;] &gt; 0){\n        \n        $result = array(\n            'code' =&gt; 201,\n            'msg' =&gt; '\u4e0a\u4f20\u5931\u8d25'\n        );\n    }else{\n        \n        \/\/ \u4e0a\u4f20\u6587\u4ef6\n        move_uploaded_file($_FILES[&quot;file&quot;][&quot;tmp_name&quot;], &quot;upload\/&quot;.$newfile.$hzm);\n        $file_url = 'http:\/\/'.$_SERVER['SERVER_NAME'].$_SERVER[&quot;REQUEST_URI&quot;];\n        $result = array(\n            &quot;code&quot; =&gt; 200,\n            &quot;msg&quot; =&gt; &quot;\u4e0a\u4f20\u6210\u529f&quot;,\n            &quot;url&quot; =&gt; dirname($file_url).&quot;\/upload\/&quot;.$newfile.$hzm\n        );\n    }\n}else{\n    \n    $result = array(\n        'code' =&gt; 202,\n        'msg' =&gt; '\u6b64\u7c7b\u6587\u4ef6\u4e0d\u80fd\u4e0a\u4f20'\n    );\n}\n\n\/\/ \u8f93\u51faJSON\necho json_encode($result, JSON_UNESCAPED_UNICODE);\n?&gt;<\/code><\/pre>\n<p>\u6ce8\u610f\uff0c\u9700\u8981\u5728\u540c\u4e00\u76ee\u5f55\u4e0b\u5efa\u7acbupload\u76ee\u5f55\u4ee5\u5b58\u653e\u4e0a\u4f20\u7684\u6587\u4ef6\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc8xd0\" alt=\"image.png\" title=\"image.png\" loading=\"lazy\"><\/p>\n<h2>\u4f5c\u8005<\/h2>\n<p>TANKING<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000043941004\">https:\/\/segmentfault.com\/a\/1190000043941004<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6548\u679c index.html &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;he [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10008002","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10008002","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10008002"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10008002\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10008002"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10008002"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10008002"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}