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., "
H:\Test\ 
".

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






Content:





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

async function read(input) {
// alert(input.files);
msg.innerText = await readFile(input.files[0]);
}
Posted
Updated 25-Nov-19 23:42pm
Comments
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.
   
Comments
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 !
   
v2
Comments
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?
Quote:
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.
   
Comments
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 :(
HTML
<pre lang="HTML"><button onclick="openFile(dispFile)">Open a file</button>




JavaScript
<script>
            function dispFile(contents) {                
                document.getElementById('contents').innerHTML=contents;
                alert(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);
                elem.dispatchEvent(eventMouse);
            }
            function openFile(func) {
                readFile = function(e) {
                    var file = e.target.files[0];
                    if (!file) {
                        return;
                    }
                    var reader = new FileReader();
                    reader.onload = function(e) {
                        var contents = e.target.result;
                        fileInput.func(contents);
                    }                    
                    reader.readAsText(file);
                }
                fileInput = document.createElement("input");
                fileInput.type='file';
                fileInput.style.display='none';
                fileInput.onchange=readFile;
                fileInput.func=func;
                document.body.appendChild(fileInput);
                clickElem(fileInput);
            }
        </script>
   
v2
Comments
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