如何(在JavaScript中,jQuery的,或者阿贾克斯和放大器;这样)我可以加载本地文本文件或XML文件到一个变量?放大器、变量、文本文件、加载

2023-09-10 18:26:58 作者:倦旅人.

首先,我知道<输入类型=文件值=C:\\ blah.txt> 将无法正常工作。我曾尝试Ajax和其他的东西,但谷歌有保障,所以我不能。我怎么可能加载本地文件中的JavaScript(而不是PHP)?不,我不是试图窃取个人数据。

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.

或者,如果这些都不能正常工作,是否有可能从命令提示符下将数据传递给一个HTML / JavaScript文件/页面,无需复杂的服务器设置? (命令提示符读取该文件,并给它的HTML)。

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).

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

我的一个失败的尝试:

One of my failed tries:

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

我曾尝试使用脚本和文本类型为JS和文本文件,但没有运气。我一直在控制台得到谷歌的安全错误。

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.

我想加载file.txt的,在文件格式。可以说我有变种X = /path/to/localfile/text.txt

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
    }
});

基本上,我想要加载从可变名称的文件,和文本文件的数据发送到变量y

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.

推荐答案

这是不可能的,因为现在。如果要加载Web项目的本地文件,你可以使用Java小程序。 使用Javascript可以访问只有cookies和网页存储。

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.

 
精彩推荐
图片推荐