{"id":8200,"date":"2023-06-27T11:40:28","date_gmt":"2023-06-27T11:40:28","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/qanda\/?p=8200"},"modified":"2024-06-27T10:27:54","modified_gmt":"2024-06-27T10:27:54","slug":"javascript-using-the-filereader-api","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/qanda\/javascript\/javascript-using-the-filereader-api","title":{"rendered":"How to Get the Filename From the Javascript FileReader: A Complete Guide"},"content":{"rendered":"<p>An example on how you can get file name using FileReader constructor in Javascript:<\/p>\n<p><strong>index.html<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">&lt;!DOCTYPE html&gt;\r\n&lt;html lang=\"en\"&gt;\r\n&lt;head&gt;\r\n    &lt;meta charset=\"UTF-8\"&gt;\r\n    &lt;meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\"&gt;\r\n    &lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"&gt;\r\n    &lt;title&gt;Document&lt;\/title&gt;\r\n    &lt;script src=\".\/script.js\"&gt;&lt;\/script&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n    &lt;main&gt;\r\n        &lt;div&gt;\r\n            &lt;label for=\"file\"&gt;Select file&lt;\/label&gt;\r\n            &lt;input type=\"file\" name=\"file\" id=\"file\"&gt;\r\n        &lt;\/div&gt;\r\n        &lt;button onclick=\"uploadFile()\"&gt;Upload&lt;\/button&gt;\r\n    &lt;\/main&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n<p><b>script.js<\/b><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">const uploadFile = () =&gt; {\r\n    const file = document.getElementById(\"file\").files[0];\r\n    const reader = new FileReader();\r\n    reader.onload = () =&gt; {\r\n        console.log(reader.fileName); \/\/ file name\r\n    };\r\n\r\n    \/\/ set file name for reader;\r\n    reader.fileName = file.name;\r\n    \r\n    \/\/ Read the file\r\n    reader.readAsDataURL(file);\r\n}<\/pre>\n<p><b>Explanation<\/b><\/p>\n<p>After selecting the file, when you click on the upload button, the uploadFile method will be executed.<\/p>\n<p>Inside the method, we are storing the selected file inside the file variable and we are providing the file variable to FileReader.<\/p>\n<p>And finally, inside the onload event handler of FileReader, We can get the name of the file.<\/p>\n<h3>Example for Multi File Input<\/h3>\n<p><b>index.html<\/b><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">&lt;!DOCTYPE html&gt;\r\n&lt;html lang=\"en\"&gt;\r\n&lt;head&gt;\r\n    &lt;meta charset=\"UTF-8\"&gt;\r\n    &lt;meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\"&gt;\r\n    &lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"&gt;\r\n    &lt;title&gt;Document&lt;\/title&gt;\r\n    &lt;script src=\".\/script.js\"&gt;&lt;\/script&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n    &lt;main&gt;\r\n        &lt;div&gt;\r\n            &lt;label for=\"file\"&gt;Select file&lt;\/label&gt;\r\n            &lt;input type=\"file\" name=\"file\" id=\"file\" multiple&gt;\r\n        &lt;\/div&gt;\r\n        &lt;button onclick=\"uploadFile()\"&gt;Upload&lt;\/button&gt;\r\n    &lt;\/main&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n<p><b>script.js<\/b><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">const uploadFile = () =&gt; {\r\n    const files = document.getElementById(\"file\").files;\r\n    for(const file of files) {\r\n        const reader = new FileReader();\r\n        reader.onload = () =&gt; {\r\n            console.log(reader.fileName); \/\/ file name\r\n        };\r\n        \/\/ set file name for reader;\r\n        reader.fileName = file.name;\r\n        \r\n        \/\/ Read the file\r\n        reader.readAsDataURL(file);\r\n    }\r\n}<\/pre>\n<p>Compared to the previous example, for multi file input, we just have to iterate a list of files, and then we are providing individual file objects into FileReader.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>An example on how you can get file name using FileReader constructor in Javascript: index.html &lt;!DOCTYPE html&gt; &lt;html lang=&#8221;en&#8221;&gt; &lt;head&gt; &lt;meta charset=&#8221;UTF-8&#8243;&gt; &lt;meta http-equiv=&#8221;X-UA-Compatible&#8221; content=&#8221;IE=edge&#8221;&gt; &lt;meta name=&#8221;viewport&#8221; content=&#8221;width=device-width, initial-scale=1.0&#8243;&gt; &lt;title&gt;Document&lt;\/title&gt; &lt;script src=&#8221;.\/script.js&#8221;&gt;&lt;\/script&gt; &lt;\/head&gt; &lt;body&gt; &lt;main&gt; &lt;div&gt; &lt;label for=&#8221;file&#8221;&gt;Select file&lt;\/label&gt; &lt;input type=&#8221;file&#8221; name=&#8221;file&#8221; id=&#8221;file&#8221;&gt; &lt;\/div&gt; &lt;button onclick=&#8221;uploadFile()&#8221;&gt;Upload&lt;\/button&gt; &lt;\/main&gt; &lt;\/body&gt; &lt;\/html&gt; script.js const uploadFile = () =&gt; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":8438,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[22],"tags":[],"class_list":["post-8200","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/8200"}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/comments?post=8200"}],"version-history":[{"count":6,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/8200\/revisions"}],"predecessor-version":[{"id":10764,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/8200\/revisions\/10764"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media\/8438"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media?parent=8200"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/categories?post=8200"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/tags?post=8200"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}