{"id":1568,"date":"2014-12-01T17:19:50","date_gmt":"2014-12-01T17:19:50","guid":{"rendered":"http:\/\/41j.com\/blog\/?p=1568"},"modified":"2014-12-02T01:19:17","modified_gmt":"2014-12-02T01:19:17","slug":"read-local-file-javascript-string","status":"publish","type":"post","link":"https:\/\/41j.com\/blog\/2014\/12\/read-local-file-javascript-string\/","title":{"rendered":"Read a local file into a Javascript string"},"content":{"rendered":"<p>I needed to grab a local file and pull it into a Javascript string, I wanted to do this without any serverside support. I hack together the following short example, the same method can be used to pull in raw files for client-side processing: <\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;input type=&quot;file&quot; id=&quot;files&quot; name=&quot;files&#x5B;]&quot; multiple \/&gt;\r\n&lt;output id=&quot;list&quot;&gt;&lt;\/output&gt;\r\n\r\n&lt;script&gt;\r\n  function handleFileSelect(evt) {\r\n    var files = evt.target.files; \/\/ FileList object\r\n\r\n    for (var i = 0, f; f = files&#x5B;i]; i++) {\r\n\r\n      var reader = new FileReader();\r\n\r\n      \/\/ Closure to capture the file information.\r\n      reader.onload = (function(theFile) {\r\n        return function(e) {\r\n          var s = String.fromCharCode.apply(null, new Uint8Array(e.target.result));\r\n          document.write(s);\r\n        };\r\n      })(f);\r\n\r\n      \/\/ Read in the file as an ArrayBuffer\r\n      reader.readAsArrayBuffer(f);\r\n    }\r\n  }\r\n\r\n  document.getElementById('files').addEventListener('change', handleFileSelect, false);\r\n&lt;\/script&gt;\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I needed to grab a local file and pull it into a Javascript string, I wanted to do this without any serverside support. I hack together the following short example, the same method can be used to pull in raw files for client-side processing: &lt;input type=&quot;file&quot; id=&quot;files&quot; name=&quot;files&#x5B;]&quot; multiple \/&gt; &lt;output id=&quot;list&quot;&gt;&lt;\/output&gt; &lt;script&gt; function handleFileSelect(evt) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[1],"tags":[],"class_list":["post-1568","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1RRoU-pi","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/1568","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/comments?post=1568"}],"version-history":[{"count":3,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/1568\/revisions"}],"predecessor-version":[{"id":1576,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/1568\/revisions\/1576"}],"wp:attachment":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/media?parent=1568"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/categories?post=1568"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/tags?post=1568"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}