Home Javascript Javascriptexamples JavaScript Open file
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

JavaScript Open file

Advertisement
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:

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

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:

        

Advertisements

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 18, 2011

Ask Questions?    Discuss: JavaScript Open file  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
Clarence
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
DMCA.com