{"id":12074,"date":"2022-09-29T13:05:45","date_gmt":"2022-09-29T07:35:45","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-reading-local-file-in-javascript-duplicate\/"},"modified":"2022-09-29T13:05:45","modified_gmt":"2022-09-29T07:35:45","slug":"solved-reading-local-file-in-javascript-duplicate","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-reading-local-file-in-javascript-duplicate\/","title":{"rendered":"[Solved] Reading local file in javascript [duplicate]"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-26199363\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"26199363\" data-parentid=\"26199028\" data-score=\"2\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>The answer is not really, no.<\/p>\n<p><strong><em>BUT there are some workarounds, depending on what you can get away with (supported browsers, etc)<\/em><\/strong><\/p>\n<p>When you grab a local file (I&#8217;m assuming you&#8217;re using <code>&lt;input type=\"file\"&gt;<\/code>, rather than partially supported, unstandardized methods), you get a <code>\"change\"<\/code> event to subscribe to, but that change reflects the change in selected files, not a change in the contents of a particular file.<\/p>\n<p>Moving beyond that, if you have a <code>Blob<\/code> or a <code>File<\/code> which inherits from <code>Blob<\/code>, you basically have a buffer full of data.<\/p>\n<p>That buffer isn&#8217;t going to dynamically update itself, based on changes elsewhere in the OS, nor does it support callback options to do so.<\/p>\n<p>Alternatively, from a blob or a file, you have the option to use <code>URL.createObjectURL( file )<\/code>, which returns a temporary url, which references a file, rather than the content of the file.  <\/p>\n<p>That said, any change to that file might then be reflected from subsequent calls to that URI.<br \/>\nThis isn&#8217;t something I&#8217;ve built to test, yet.  <\/p>\n<p>There would be no callback; however, you could set up an interval-based heartbeat, to make an XHR call to that URI, and then compare the <code>.responseText<\/code> with the previous <code>.responseText<\/code>.<\/p>\n<p>Again, no guarantees, but that&#8217;s as close as I think you&#8217;re going to get to an answer, right now (while still in the realm of plugin\/extension-free solutions), and might just be worth an hour of working out a prototype, and a couple more, testing support (whether the link is live, rather than an initial representation, etc).<\/p>\n<pre><code>var input = document.querySelector(\"input[type=\"file\"]\"), \/\/ client must open file, themselves\n    fileHandle = \"\", \/\/ this will be the url for the local file\n    content    = \"\", \/\/ this will be the latest contents of the file\n    previousContent = \"\"; \/\/ this will be the previous contents\n\nvar ms = 1000,\n    secs = 2,\n\n    pollId; \/\/ will be the process ID for the polling (so you can stop checking for changes, later).\n\n\/\/ wait for a file to be selected\ninput.addEventListener(\"change\", function (evt) {\n\n    var file = evt.target.files[0]; \/\/ grab the file object\n    fileHandle = URL.createObjectURL(file); \/\/ create a url which points to the local file\n\n    \/\/ create an interval to check for changes\n    pollId = setInterval(\n        getLatest(fileHandle, getCurrent, updateCurrent),\n        secs * ms\n     );\n});\n\nfunction updateCurrent (latest) {\n    current = latest;\n    doStuff(current);\n    \/\/ the text has changed; it's been updated;\n    \/\/ now call home or do something else\n}\n\nfunction getCurrent () { return current; }\n\nfunction getLatest (url, getCurrent, onchange) {\n    return function () {\n        \/\/ this isn't actually calling the network\n        \/\/ it's calling the local file\n        var xhr = new XMLHttpRequest();\n        xhr.open(\"GET\", url);\n\n        xhr.onload = function () {\n            var latest = xhr.responseText;\n            if (getCurrent() !== latest) { onchange(latest); }\n        };\n\n        xhr.send();\n    };\n}\n<\/code><\/pre>\n<p>You now have change polling, in supporting browsers.<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">1<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Reading local file in javascript [duplicate] <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] The answer is not really, no. BUT there are some workarounds, depending on what you can get away with (supported browsers, etc) When you grab a local file (I&#8217;m assuming you&#8217;re using &lt;input type=&#8221;file&#8221;&gt;, rather than partially supported, unstandardized methods), you get a &#8220;change&#8221; event to subscribe to, but that change reflects the change &#8230; <a title=\"[Solved] Reading local file in javascript [duplicate]\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-reading-local-file-in-javascript-duplicate\/\" aria-label=\"More on [Solved] Reading local file in javascript [duplicate]\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[575,333,1224],"class_list":["post-12074","post","type-post","status-publish","format-standard","hentry","category-solved","tag-file-io","tag-javascript","tag-local-storage"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Reading local file in javascript [duplicate] - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-reading-local-file-in-javascript-duplicate\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Reading local file in javascript [duplicate] - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] The answer is not really, no. BUT there are some workarounds, depending on what you can get away with (supported browsers, etc) When you grab a local file (I&#8217;m assuming you&#8217;re using &lt;input type=&quot;file&quot;&gt;, rather than partially supported, unstandardized methods), you get a &quot;change&quot; event to subscribe to, but that change reflects the change ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-reading-local-file-in-javascript-duplicate\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-09-29T07:35:45+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-reading-local-file-in-javascript-duplicate\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-reading-local-file-in-javascript-duplicate\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Reading local file in javascript [duplicate]\",\"datePublished\":\"2022-09-29T07:35:45+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-reading-local-file-in-javascript-duplicate\/\"},\"wordCount\":278,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"file-io\",\"javascript\",\"local-storage\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-reading-local-file-in-javascript-duplicate\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-reading-local-file-in-javascript-duplicate\/\",\"name\":\"[Solved] Reading local file in javascript [duplicate] - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-09-29T07:35:45+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-reading-local-file-in-javascript-duplicate\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-reading-local-file-in-javascript-duplicate\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-reading-local-file-in-javascript-duplicate\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Reading local file in javascript [duplicate]\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Reading local file in javascript [duplicate] - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-reading-local-file-in-javascript-duplicate\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Reading local file in javascript [duplicate] - JassWeb","og_description":"[ad_1] The answer is not really, no. BUT there are some workarounds, depending on what you can get away with (supported browsers, etc) When you grab a local file (I&#8217;m assuming you&#8217;re using &lt;input type=\"file\"&gt;, rather than partially supported, unstandardized methods), you get a \"change\" event to subscribe to, but that change reflects the change ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-reading-local-file-in-javascript-duplicate\/","og_site_name":"JassWeb","article_published_time":"2022-09-29T07:35:45+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-reading-local-file-in-javascript-duplicate\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-reading-local-file-in-javascript-duplicate\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Reading local file in javascript [duplicate]","datePublished":"2022-09-29T07:35:45+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-reading-local-file-in-javascript-duplicate\/"},"wordCount":278,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["file-io","javascript","local-storage"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-reading-local-file-in-javascript-duplicate\/","url":"https:\/\/jassweb.com\/solved\/solved-reading-local-file-in-javascript-duplicate\/","name":"[Solved] Reading local file in javascript [duplicate] - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-09-29T07:35:45+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-reading-local-file-in-javascript-duplicate\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-reading-local-file-in-javascript-duplicate\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-reading-local-file-in-javascript-duplicate\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Reading local file in javascript [duplicate]"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/12074","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=12074"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/12074\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=12074"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=12074"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=12074"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}