简体   繁体   中英

How (In javascript, Jquery, or Ajax & such) can I load a local text file or xml file into a variable?

First, I know <input type="file" value="c:\\\\blah.txt"> won't work. I have tried Ajax, and other things, but Google has security, so I can't. How could I possibly load a local file in JavaScript (Not PHP)? No, i'm not trying to steal people's data.

Or, if none of this will work, is it possible to pass data from a command prompt to a HTML/JavaScript file/page without a complicated server setup? (The command prompt reads the file, and gives it to the HTML).

What I am not trying to do:

<script type="text/javascript" src="//www.mydomain.com/scriptfile.js">

One of my failed tries:

$.ajax({
    type: "GET",
    url: path,
    dataType: "script",
});

I have tried to use the script and text type for both JS and text files, but no luck. I keep getting Google security errors in the console.

What I am trying to do:

I want to load a file.txt in the "File" format. Lets say I have var x = /path/to/localfile/text.txt .

Then, I want to do basically:

var x = "c:\\folder\\test.txt";
$.ajax({
    async: false,
    type: "GET",
    url: x, //<------
    dataType: "text",
    contentType: "application/x-www-form-urlencoded;charset=UTF-8",
    success: function (data) {
        alert(data);
        //parse the file content here
    }
});

Basically, I want to load a file from a variable name, and send the data of the text file to variable y.

But how ever many methods I try, they don't work.

This is impossible, for now. If you want to load local file in web project you can use java applet. Javascript gives access only to cookies and web storage.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM