how to read local file via using js in the browser

Three Sentences

  • On default, it‘s prohibited to read the local file on the browser
  • Need to set the browser (add some launching parameter) first if we still want to read the local file
  • Using jQuery but not original web-api

Project Structure

project readLocalFileByJQueryOnFirefoxBrowser
   - index.html
   - loadLocalFile.js
   - htmlEle.txt

index.html

<html>
	<head>
		<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.1.1.min.js"></script>
	</head>
	<body>
		<div id="myContainer">
		</div>
		
		<script src="loadLocalFile.js"></script>
	</body>
</html>

loadLocalFile.js

$.get(‘htmlEle.txt‘, function(data) { 
	console.log("p")
    var lines = data.split("\n");

    $.each(lines, function(n, elem) {
       $(‘#myContainer‘).append(‘<div>‘ + elem + ‘</div>‘);
    });
});

htmlEle.txt

<h1>I will give the service you asked</h1>

Set Chrome-browser

  • find the chrome icon in win-os
  • right-click
  • on the panel "Google Chrome Properties", find the textarea called target
  • its value is "C:\Program Files\Google\Chrome\Application\chrome.exe"
  • set it as "C:\Program Files\Google\Chrome\Application\chrome.exe" --allow-file-access-from-files

Open Chrome by this icon and type the html-path, then we‘ll win the victory

Final Fact: skipped

how to read local file via using js in the browser

上一篇:jQuery2021.3.11日


下一篇:kubernetes生产实践之redis-cluster