How to open a local disk file with JavaScript?

前端 未结 9 2007
[愿得一人]
[愿得一人] 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:42

    Because I have no life and I want those 4 reputation points so I can show my love to (upvote answers by) people who are actually good at coding I've shared my adaptation of Paolo Moretti's code. Just use openFile(function to be executed with file contents as first parameter).

    function dispFile(contents) {
      document.getElementById('contents').innerHTML=contents
    }
    function clickElem(elem) {
    	// Thx user1601638 on Stack Overflow (6/6/2018 - https://stackoverflow.com/questions/13405129/javascript-create-and-save-file )
    	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)
    			document.body.removeChild(fileInput)
    		}
    		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)
    }
    Click the button then choose a file to see its contents displayed below.
    <button onclick="openFile(dispFile)">Open a file</button>
    <pre id="contents"></pre>

    0 讨论(0)
  • 2020-11-22 01:45

    Consider reformatting your file into javascript. Then you can simply load it using good old...

    <script src="thefileIwantToLoad.js" defer></script>
    
    0 讨论(0)
  • 2020-11-22 01:46

    The HTML5 fileReader facility does allow you to process local files, but these MUST be selected by the user, you cannot go rooting about the users disk looking for files.

    I currently use this with development versions of Chrome (6.x). I don't know what other browsers support it.

    0 讨论(0)
  • 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]);
    }
    <input type="file" onchange="read(this)"/>
    <h3>Content:</h3><pre id="msg"></pre>

    0 讨论(0)
  • 2020-11-22 01:54

    You can't. New browsers like Firefox, Safari etc. block the 'file' protocol. It will only work on old browsers.

    You'll have to upload the files you want.

    0 讨论(0)
  • 2020-11-22 01:56

    The xmlhttp request method is not valid for the files on local disk because the browser security does not allow us to do so.But we can override the browser security by creating a shortcut->right click->properties In target "... browser location path.exe" append --allow-file-access-from-files.This is tested on chrome,however care should be taken that all browser windows should be closed and the code should be run from the browser opened via this shortcut.

    0 讨论(0)
提交回复
热议问题