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

断了今生、忘了曾经 提交于 2019-12-01 12:57:56

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/30135589/how-in-javascript-jquery-or-ajax-such-can-i-load-a-local-text-file-or-xml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!