How to open a local disk file with JavaScript?

前端 未结 9 2037
[愿得一人]
[愿得一人] 2020-11-22 01:21

I tried to open file with

window.open(\"file:///D:/Hello.txt\");

The browser does not allow opening a local file this way, probably for sec

9条回答
  •  逝去的感伤
    2020-11-22 01:54

    Try

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

    but user need to take action to choose file

    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) {
      msg.innerText = await readFile(input.files[0]);
    }
    
    

    Content:

提交回复
热议问题