{"id":4525,"date":"2015-09-25T11:42:47","date_gmt":"2015-09-25T02:42:47","guid":{"rendered":"https:\/\/gunmagisgeek.com\/wordpress\/?p=4525"},"modified":"2023-01-05T14:25:47","modified_gmt":"2023-01-05T05:25:47","slug":"post-4525","status":"publish","type":"post","link":"https:\/\/gunmagisgeek.com\/blog\/javascript\/4525","title":{"rendered":"JavaScript\u3067Shift_JIS\u306ecsv\u3092\u8aad\u307f\u8fbc\u3080\uff08\u6587\u5b57\u5316\u3051\u5bfe\u7b56\uff09"},"content":{"rendered":"<p><a href=\"https:\/\/gunmagisgeek.com\/blog\/d3-js\/4515\">\u697d\u5929\u30ab\u30fc\u30c9\u306e\u5229\u7528\u660e\u7d30csv\u3092\u8aad\u307f\u3053\u3093\u3067\u30c4\u30ea\u30fc\u30de\u30c3\u30d7\u3092\u8868\u793a\u3059\u308b<\/a>\u30b3\u30fc\u30c9\u3092\u66f8\u3044\u3066\u307f\u305f\u308f\u3051\u3067\u3059\u304c\u3001\u660e\u7d30csv\u306e\u6587\u5b57\u30b3\u30fc\u30c9\u304cShiftJIS\u3060\u3063\u305f\u305f\u3081\u3001\u601d\u3063\u3066\u3044\u305f\u3088\u308a\u624b\u9593\u53d6\u308a\u307e\u3057\u305f\u3002<\/p>\n<p>\u5099\u5fd8\u9332\u3068\u3057\u3066\u30e1\u30e2\u3063\u3066\u304a\u304d\u307e\u3059\u3002<\/p>\n<h2>csv\u8aad\u307f\u8fbc\u307f<\/h2>\n<p>UTF8\u306ecsv\u3067\u3042\u308c\u3070\u3001\u4e0b\u8a18\u306e\u65b9\u6cd5\u3067input type=file\u304b\u3089\u8aad\u307f\u8fbc\u3080\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre class=\"lang:js decode:true \">var fileBtn = document.querySelector(\"#fileBtn\");\nfileBtn.addEventListener('change', upload, false);\n\nfunction upload(evt) {\n\tif (!isFileUpload()) {\n\t\tconsole.log('\u304a\u4f7f\u3044\u306e\u30d6\u30e9\u30a6\u30b6\u306f\u30d5\u30a1\u30a4\u30ebAPI\u304c\u30b5\u30dd\u30fc\u30c8\u3055\u308c\u3066\u3044\u307e\u305b\u3093');\n\t} else {\n\t\tvar data = null;\n\t\tvar file = evt.target.files[0];\n\t\tvar reader = new FileReader();\n\t\treader.reader.readAsText(file);\n\t\treader.onload = function(event) {\n\t\t\tvar result = event.target.result;\n\t\t\tconsole.log(result) \/\/-&gt; csv\u30c7\u30fc\u30bf(string)\n\t\t};\n\t\treader.onerror = function() {\n\t\t\tconsole.log('\u30d5\u30a1\u30a4\u30eb\u304c\u8aad\u307f\u8fbc\u3081\u307e\u305b\u3093\u3067\u3057\u305f\u3002 ' + file.fileName);\n\t\t};\n\t}\n}\n\nfunction isFileUpload() {\n\t   var isCompatible = false;\n\t   if (window.File &amp;&amp; window.FileReader &amp;&amp; window.FileList &amp;&amp; window.Blob) {\n\t   isCompatible = true;\n\t   }\n\t   return isCompatible;\n}<\/pre>\n<p>\u3057\u304b\u3057\u3001csv\u306e\u6587\u5b57\u30b3\u30fc\u30c9\u304cshiftJIS\u3060\u3068\u3001\u51fa\u529b\u7d50\u679c\u304c\u6587\u5b57\u5316\u3051\u3057\u307e\u3059\u3002<\/p>\n<h2>\u6587\u5b57\u30b3\u30fc\u30c9\u5909\u63db\u30e9\u30a4\u30d6\u30e9\u30ea<\/h2>\n<p>JavaScript\u3067\u6587\u5b57\u30b3\u30fc\u30c9\u3092\u5909\u63db\u3059\u308b\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u4f5c\u6210\u3055\u308c\u3066\u3044\u308b\u65b9\u304c\u3044\u305f\u306e\u3067\u3001\u4eca\u56de\u306f\u3053\u3061\u3089\u3092\u4f7f\u308f\u305b\u3066\u3044\u305f\u3060\u304d\u307e\u3057\u305f\u3002<\/p>\n<p><a href=\"http:\/\/polygon-planet-log.blogspot.jp\/2012\/04\/javascript.html\">JavaScript\u3067\u6587\u5b57\u30b3\u30fc\u30c9\u5909\u63db\u30e9\u30a4\u30d6\u30e9\u30ea\u4f5c\u3063\u3066\u307f\u305f | \u5727\u7e2e\u96fb\u5b50\u3069\u3046\u306e\u3053\u3046\u306e<\/a><\/p>\n<p>\u305f\u3060\u4e00\u70b9\u6ce8\u610f\u3059\u3079\u304d\u30dd\u30a4\u30f3\u30c8\u304c\u3042\u3063\u3066\u3001FileReader\u3067\u30c7\u30fc\u30bf\u3092\u8aad\u307f\u8fbc\u3080\u969breadAsText\u30e1\u30bd\u30c3\u30c9\u3067\u306f\u306a\u304freadAsBinaryString\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u3046\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u3053\u308c\u306b\u6c17\u4ed8\u304f\u307e\u3067\u306b\u7d50\u69cb\u6642\u9593\u304c\u304b\u304b\u308a\u307e\u3057\u305f orz<\/p>\n<h2>csv(shiftJIS)\u8aad\u307f\u8fbc\u307f<\/h2>\n<p>ShiftJIS\u306ecsv\u30c7\u30fc\u30bf\u3092\u8aad\u307f\u8fbc\u3080\u30b5\u30f3\u30d7\u30eb\u306f\u4ee5\u4e0b\u3068\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre class=\"lang:js decode:true \">var fileBtn = document.querySelector(\"#fileBtn\");\nfileBtn.addEventListener('change', upload, false);\n\nfunction upload(evt) {\n\tif (!isFileUpload()) {\n\t\tconsole.log('\u304a\u4f7f\u3044\u306e\u30d6\u30e9\u30a6\u30b6\u306f\u30d5\u30a1\u30a4\u30ebAPI\u304c\u30b5\u30dd\u30fc\u30c8\u3055\u308c\u3066\u3044\u307e\u305b\u3093');\n\t} else {\n\t\tvar data = null;\n\t\tvar file = evt.target.files[0];\n\t\treader = new FileReader();\n\t\treader.readAsBinaryString(file);\n\t\treader.onload = function(event) {\n\t\t\tvar result = event.target.result;\n\t\t\tvar sjisArray = str2Array(result);\n\t\t\tvar uniArray = Encoding.convert(sjisArray, 'UNICODE', 'SJIS');\n\t\t\tvar result = Encoding.codeToString(uniArray);\n\t\t\tconsole.log(result); \/\/csv\u30c7\u30fc\u30bf(string)\n\n\t\t};\n\t\treader.onerror = function() {\n\t\t\tconsole.log('\u30d5\u30a1\u30a4\u30eb\u304c\u8aad\u307f\u8fbc\u3081\u307e\u305b\u3093\u3067\u3057\u305f\u3002 ' + file.fileName);\n\t\t};\n\t}\n}\n\nfunction isFileUpload() {\n\t   var isCompatible = false;\n\t   if (window.File &amp;&amp; window.FileReader &amp;&amp; window.FileList &amp;&amp; window.Blob) {\n\t   isCompatible = true;\n\t   }\n\t   return isCompatible;\n}\n\nfunction str2Array(str) {\n    var array = [],i,il=str.length;\n    for(i=0;i&lt;il;i++) array.push(str.charCodeAt(i));\n    return array;\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u697d\u5929\u30ab\u30fc\u30c9\u306e\u5229\u7528\u660e\u7d30csv\u3092\u8aad\u307f\u3053\u3093\u3067\u30c4\u30ea\u30fc\u30de\u30c3\u30d7&hellip;<\/p>\n","protected":false},"author":1,"featured_media":4523,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-4525","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript","has-post-thumbnail-archive"],"_links":{"self":[{"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/posts\/4525","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/comments?post=4525"}],"version-history":[{"count":1,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/posts\/4525\/revisions"}],"predecessor-version":[{"id":8146,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/posts\/4525\/revisions\/8146"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/media\/4523"}],"wp:attachment":[{"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/media?parent=4525"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/categories?post=4525"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/tags?post=4525"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}