Click here to Skip to main content
14,924,138 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello All,

I am suppose to read the local drive file but I was not allowed to use Input type ="file" html tag as the file path will be hardcoded in code Eg., "

I have tried many options in HTML and Javascript but could not succeed. Please help me.

What I have tried:

<title>File Upload and Download


function readFile(file) {
return new Promise((resolve, reject) => {
let fr = new FileReader();
fr.onload = x=> resolve(fr.result);

async function read(input) {
// alert(input.files);
msg.innerText = await readFile(input.files[0]);
Updated 25-Nov-19 23:42pm
phil.o 23-Nov-19 6:31am
If you are trying to read a file on the client computer from the server, that will not be possible.

Code running in the browser has no access to the client machines resource, including the file system. Your code can NOT read files in the client file system.
Raaditya 26-Nov-19 5:47am
Thanks for your time. It actually does but with client interaction/permission. The code below shows that work.
See example here, it also has a "Run code snippet" button to try it out: How to open a local disk file with JavaScript? - Stack Overflow[^]
Note that this only works by using a file selection dialog and not with a hard coded string !
Raaditya 26-Nov-19 5:39am
Your help was very much appreciated, that code does the work. The only thing is could not select multiple files, were as in we can choose multiple files. Do you have any idea sir?
I have tried many options in HTML and Javascript but could not succeed.

Yesn it is the principle: a webapp is not allowed to read client HDD, it is a matter of security.
At best, the webapp ask the client to read a client, the client choose if it allow it and which file will be read.
Raaditya 26-Nov-19 5:51am
what you said is right. Problem is when you are preparing a desktop app with open source technology you are forced to do some kind of stuff like copy/move files in client local drive. Eg., Download patch files from webserver and locate it in customer drive. Then the files need to be moved inside Database located in client machine. Not sure if the above said points were clear. But this is how I was stuck :(
<pre lang="HTML"><button onclick="openFile(dispFile)">Open a file</button>

            function dispFile(contents) {                
            function clickElem(elem) {                
                var eventMouse = document.createEvent("MouseEvents");
                eventMouse.initMouseEvent("click", true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
            function openFile(func) {
                readFile = function(e) {
                    var file =[0];
                    if (!file) {
                    var reader = new FileReader();
                    reader.onload = function(e) {
                        var contents =;
                fileInput = document.createElement("input");
Raaditya 26-Nov-19 5:46am
I hope this may help someone in future. But it would be great if someone helps in choosing multiple files in a folder for copying/moving it to other directory. Note: The above provided code read the file and print it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900