{"id":2330,"date":"2021-02-11T18:00:24","date_gmt":"2021-02-11T09:00:24","guid":{"rendered":"https:\/\/mattyan.net\/blog\/?p=2330"},"modified":"2021-02-11T18:00:24","modified_gmt":"2021-02-11T09:00:24","slug":"javascrip%e3%81%a0%e3%81%91%e3%81%a7%e3%83%95%e3%82%a1%e3%82%a4%e3%83%ab%e3%81%ae%e3%83%80%e3%82%a6%e3%83%b3%e3%83%ad%e3%83%bc%e3%83%89%e3%82%92%e3%82%84%e3%81%a3%e3%81%a6%e3%81%bf%e3%81%9f","status":"publish","type":"post","link":"https:\/\/mattyan.net\/blog\/?p=2330","title":{"rendered":"Javascrip\u3060\u3051\u3067\u30d5\u30a1\u30a4\u30eb\u306e\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3092\u3084\u3063\u3066\u307f\u305f"},"content":{"rendered":"<p>\u5199\u771f\u516c\u958b\u30d7\u30ed\u30b0\u30e9\u30e0ver5\u306e\u958b\u767a\u4e2d\u3002\u5165\u529b\u60c5\u5831\u306e\u4e00\u6642\u4fdd\u5b58\u306e\u65b9\u6cd5\u3092\u8abf\u3079\u3066\u305f\u3089\u3001Javascript\u3067\u30d5\u30a1\u30a4\u30eb\u306e\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u304c\u3067\u304d\u308b\u3063\u307d\u3044\u306e\u3067\u3084\u3063\u3066\u307f\u305f\u3002<br \/>\n<!--more--><br \/>\n\u65b9\u6cd5\u3068\u3057\u3066\u306f\u3001<a href=\"https:\/\/developer.mozilla.org\/ja\/docs\/Web\/HTML\/Element\/a\">a\u30bf\u30b0\u306bdownload\u5c5e\u6027\u3092\u8ffd\u52a0\u3057\u3066\u3044\u3044\u611f\u3058\u306b\u8a2d\u5b9a\u3059\u308b<\/a>\u3068\u3044\u3044\u3089\u3057\u3044\u3002<br \/>\n\u306a\u306e\u3067\u65e9\u901f\u3084\u3063\u3066\u307f\u305f<\/p>\n<pre class=\"lang:typescript\">const compressedData = pako.gzip(data);\n\/\/ \u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\nconst downloadURL = URL.createObjectURL(new Blob([compressedData], { type: 'application\/gzip' }));\nconst downloadLink = document.createElement('a');\ndocument.body.appendChild(downloadLink);\ndownloadLink.download = 'data.gz';\ndownloadLink.href = downloadURL;\ndownloadLink.click();\ndownloadLink.remove();\nURL.revokeObjectURL(downloadURL);\n<\/pre>\n<p>data\u306b\u306f\u4fdd\u5b58\u3057\u305f\u3044\u60c5\u5831\u3092\u5165\u308c\u3066\u304a\u304f\u3002\u30c7\u30fc\u30bf\u306f\u3067\u304b\u3044\u306e\u3067<a href=\"https:\/\/www.npmjs.com\/package\/pako\">pako<\/a>\u3067gzip\u5727\u7e2e\u3002<br \/>\n<a href=\"https:\/\/developer.mozilla.org\/ja\/docs\/Web\/API\/URL\/createObjectURL\">URL.createObjectURL<\/a>\u3067\u5727\u7e2e\u30c7\u30fc\u30bf\u3092\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3059\u308b\u305f\u3081\u306eURL\u3092\u4f5c\u6210\u3002<br \/>\na\u30bf\u30b0\u3092\u4f5c\u3063\u3066body\u306b\u914d\u7f6e\u3002download\u306b\u30d5\u30a1\u30a4\u30eb\u540d\u3092\u3001href\u306bcreateObjectURL\u306e\u623b\u308a\u5024\u3092\u30bb\u30c3\u30c8\u3002<br \/>\n\u305d\u308c\u3092.click()\u3067\u30af\u30ea\u30c3\u30af\u30a4\u30d9\u30f3\u30c8\u3092\u767a\u706b\u3055\u305b\u308b\u3068\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u78ba\u8a8d\u306e\u30c0\u30a4\u30a2\u30ed\u30b0\u304c\u51fa\u308b\u3002<br \/>\n\u30a4\u30d9\u30f3\u30c8\u3092\u767a\u706b\u3055\u305b\u305f\u3089\u3001remove\u3067a\u30bf\u30b0\u3092\u524a\u9664\u3057\u3066<a href=\"https:\/\/developer.mozilla.org\/ja\/docs\/Web\/API\/URL\/revokeObjectURL\">URL.revokeObjectURL<\/a>\u3067URL\u3092\u524a\u9664\u3057\u3066\u5b8c\u4e86\u3002<br \/>\n\u304a\u307e\u3051\uff1a\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3057\u305f\u30d5\u30a1\u30a4\u30eb\u306e\u5fa9\u5143\u3002<\/p>\n<pre class=\"lang:typescript\">const upload = (event: React.DragEvent) =&gt; {\n        event.stopPropagation();\n        event.preventDefault();\n        const uploadFiles = Array.from(event.dataTransfer.files) as File[];\n        if (\/.gz$\/.test(uploadFiles[0].name)) {\n            \/\/ \u4e00\u6642\u4fdd\u5b58\u306e\u30ea\u30b9\u30c8\u30a2\n            const storeFile = uploadFiles[0];\n            const reader = new FileReader();\n            reader.onload = (result) =&gt; {\n                const decompressedData = new TextDecoder('utf-8')\n                    .decode(pako.ungzip(new Uint8Array(result.target.result as ArrayBuffer)));\n                const restoreData: RestoreData = JSON.parse(decompressedData);\n                setPhotoList(restoreData.data);\n            };\n            reader.readAsArrayBuffer(storeFile);\n        }\n}<\/pre>\n<pre class=\"lang:tsx\">&lt;section className=\"upload\"&gt;\n    &lt;div\n        onDragEnter={(e: React.DragEvent) =&gt; { e.stopPropagation(); e.preventDefault(); }}\n        onDragOver={(e: React.DragEvent) =&gt; { e.stopPropagation(); e.preventDefault(); }}\n        onDrop={upload}\n        style={{ height: '10em', backgroundColor: '#cccccc' }}\n    \/&gt;\n&lt;\/section&gt;<\/pre>\n<p>div\u306eondrop\u30a4\u30d9\u30f3\u30c8\u30cf\u30f3\u30c9\u30e9\u3067gz\u30d5\u30a1\u30a4\u30eb\u306e\u30c9\u30e9\u30c3\u30b0\u30a2\u30f3\u30c9\u30c9\u30ed\u30c3\u30d7\u3092\u53d7\u4ed8\u3002<br \/>\n<a href=\"https:\/\/developer.mozilla.org\/ja\/docs\/Web\/API\/FileReader\">FileReader<\/a>\u306e<a href=\"https:\/\/developer.mozilla.org\/ja\/docs\/Web\/API\/FileReader\/readAsArrayBuffer\">readAsArrayBuffer<\/a>\u3067\u8aad\u307f\u8fbc\u3093\u3060\u30c7\u30fc\u30bf\u3092pako\u3067\u30c7\u30b3\u30fc\u30c9\u3057\u3066state\u306b\u5165\u308c\u3066React\u306b\u30ec\u30f3\u30c0\u30ea\u30f3\u30b0\u8981\u6c42\u3057\u3066\u5b8c\u4e86\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5199\u771f\u516c\u958b\u30d7\u30ed\u30b0\u30e9\u30e0ver5\u306e\u958b\u767a\u4e2d\u3002\u5165\u529b\u60c5\u5831\u306e\u4e00\u6642\u4fdd\u5b58\u306e\u65b9\u6cd5\u3092\u8abf\u3079\u3066\u305f\u3089\u3001Javascript\u3067\u30d5\u30a1\u30a4\u30eb\u306e\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u304c\u3067\u304d\u308b\u3063\u307d\u3044\u306e\u3067\u3084\u3063\u3066\u307f\u305f\u3002<\/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-2330","post","type-post","status-publish","format-standard","hentry","category-3"],"_links":{"self":[{"href":"https:\/\/mattyan.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2330","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mattyan.net\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mattyan.net\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mattyan.net\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mattyan.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2330"}],"version-history":[{"count":0,"href":"https:\/\/mattyan.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2330\/revisions"}],"wp:attachment":[{"href":"https:\/\/mattyan.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2330"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mattyan.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2330"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mattyan.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2330"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}