JavaScript Open file

This page discusses - JavaScript Open file

JavaScript Open file


In this section, you will learn how to open and read the file in JavaScript.

You can see in the given example, we have created a file component to let the user select the file to be read and a button to submit the request. For this, we have used  <input type="file">, through which we can browse the file and get the appropriate file to read. On clicking the "Open File" button, the function openFile() is called where we have used the JavaScript property window.location, which allows to access the selected file. The document.form.selectedFile.value display the text of the selected file on the browser.

Here is the code:

<h2>Read File in Javascript</h2>
<script language="JavaScript">
function openFile() {
window.location= 'file:///' + document.form.selectedFile.value;
<form name="form">
<input type="file" name="selectedFile">
<input type=button onClick="openFile()" value="Open File">

Output will be displayed as:

Now click the browse button and select the file:

When you click the Open File button, you will get the text of the selected file:

Download Source Code:


Share on Google+Share on Google+

JavaScript Open file

Posted on: April 18, 2011 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: JavaScript Open file  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
June 19, 2012
Opening a file with javascript

Hi Thanks for sharing a script to browse afile and open it. I would like to do a similar task but without having to browse the file location. I need the file to open when a button is clicked. The file to be open should be selected by created date/time. How can that be achieved?
ashish ringe
September 25, 2012
query about the respected subject

hello sir please tell me how to get the path of the selected file in the case of JavascriptOpenFile