jQuery Javascript .get method for reading text files - Stack Overflow

I just started working on my school assignment with some regular expressions in Javascript. I can'

I just started working on my school assignment with some regular expressions in Javascript. I can't seem to figure out how to actually read data from a text file into variable using jQuery method .get(). When I try my code out nothing happens. It seems like it never enters .get() section. Here is my code:

JS file:

document.getElementById('file').onchange = function(){
var file = "New Text Document.txt";  //this will later be the selected file

$.get(file,function(data){
    var myVar = data;
    $("#123").html(myVar);
});
};

HTML file:

<!DOCTYPE html>
<html>
<head>
<title>animacija</title>
<meta charset="UTF-8">
<script src=".1.1/jquery.min.js"></script>
</head>
<body>

<input type="file" name="file" id="file">
<script type="text/javascript" src="func.js"></script>

<div id="123"></div>

</body>
</html>

I just started working on my school assignment with some regular expressions in Javascript. I can't seem to figure out how to actually read data from a text file into variable using jQuery method .get(). When I try my code out nothing happens. It seems like it never enters .get() section. Here is my code:

JS file:

document.getElementById('file').onchange = function(){
var file = "New Text Document.txt";  //this will later be the selected file

$.get(file,function(data){
    var myVar = data;
    $("#123").html(myVar);
});
};

HTML file:

<!DOCTYPE html>
<html>
<head>
<title>animacija</title>
<meta charset="UTF-8">
<script src="https://ajax.googleapis./ajax/libs/jquery/3.1.1/jquery.min.js"></script>
</head>
<body>

<input type="file" name="file" id="file">
<script type="text/javascript" src="func.js"></script>

<div id="123"></div>

</body>
</html>
Share asked Mar 2, 2017 at 16:56 LukiLuki 41911 silver badges28 bronze badges 4
  • Possible duplicate of jquery - Read a text file? – Liam Commented Mar 2, 2017 at 17:00
  • Are you viewing this HTML page in your browser as a server-hosted http: (or https:) page, or as a file: document? If http:, is the text resource you're trying to read also hosted on that same server? – apsillers Commented Mar 2, 2017 at 17:01
  • As a file:, and text resource is in the same directory as my http and js document. – Luki Commented Mar 2, 2017 at 17:03
  • Seems like exactly this was the error... I put my files on a uwamp server and now it works as the protocol is localhost: thank you :) – Luki Commented Mar 2, 2017 at 17:10
Add a ment  | 

3 Answers 3

Reset to default 5

The code snippet seems to be ok, but it will not work locally since $.get is for ajax requests and requires full available server path.

I rather remend you the use of FileReader API.

HTML

<title>animacija</title>
<script src="https://ajax.googleapis./ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<body>
  <input type="file" name="file" id="file">
  <div id="123"></div>
</body>

JavaScript

document.getElementById('file').onchange = function() {

  var file = this.files[0];
  var FR = new FileReader();

  FR.readAsText(file);
  FR.onload = function(data) {
    var myVar = data.target.result;
    $("#123").html(myVar);
  }
};

JSFiddle

Hope it works for you!

Most browsers will not allow file: resources to read other local files. This is to prevent attacks where a downloaded HTML document could start reading (and transmitting) the contents of your hard drive (or at least your Downloads folder) as soon as you open it.

The other thing you need to know here is that $.get is used for getting resources from an HTTP server, while file inputs are used to allow a user to select a file from their local drive. I realize in your case it's a little confusing, because your web page is on your local hard drive, but imagine if your HTML and scripts were being hosted online, and some other user (not you) was using them to process their own locally-stored files.

MDN has a good tutorial on how to get started with <input type="file"> inputs.

The code won't work locally due to cross-origin limitations.
It works fine when run on a remote server and all files are in the same folder.

If you want to read local files (aka. files selected by user through the file input) you can read more about FileAPI here:

https://www.html5rocks./en/tutorials/file/dndfiles/

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742291816a4416335.html

相关推荐

  • jQuery Javascript .get method for reading text files - Stack Overflow

    I just started working on my school assignment with some regular expressions in Javascript. I can'

    18小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信