首页 > Web开发 > 详细

how to read local file via using js in the browser

时间:2021-03-11 10:20:40      阅读:26      评论:0      收藏:0      [点我收藏+]

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

原文:https://www.cnblogs.com/1river/p/14515332.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!