简体   繁体   English

我需要从javascript中读取文本文件

[英]I need to read a text file from a javascript

I'm writing webpage with a javascript to read data files in text format from the server per user request. 我正在编写带有javascript的网页,以便根据用户请求从服务器读取文本格式的数据文件。 Once the text file has been loaded, I need to manipulate the data somewhat. 加载文本文件后,我需要稍微操纵一下数据。

I have been using XMLHttpRequest for the loading, however, now I see that synchronous requests are "deprecated". 我一直在使用XMLHttpRequest进行加载,但是现在我看到同步请求被“弃用”了。 I can't start manipulating the data before it's loaded, so what can I do in this case? 我无法在加载数据之前开始操作数据,所以在这种情况下我该怎么办?

Use an a synchronous request (or fetch , see below, which is also asynchronous): 使用同步请求 (或fetch ,见下文,这也是异步):

function doGET(path, callback) {
    var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function() {
        if (xhr.readyState == 4) {
            // The request is done; did it work?
            if (xhr.status == 200) {
                // ***Yes, use `xhr.responseText` here***
                callback(xhr.responseText);
            } else {
                // ***No, tell the callback the call failed***
                callback(null);
            }
        }
    };
    xhr.open("GET", path);
    xhr.send();
}

function handleFileData(fileData) {
    if (!fileData) {
        // Show error
        return;
    }
    // Use the file data
}

// Do the request
doGET("/path/to/file", handleFileData);

Or using promises, which are the more modern way to handle callbacks (but keep reading): 或者使用promises,这是处理回调的更现代的方式(但继续阅读):

 function doGET(path, callback) { return new Promise(function(resolve, reject) { var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function() { if (xhr.readyState == 4) { // The request is done; did it work? if (xhr.status == 200) { // Yes, use `xhr.responseText` to resolve the promise resolve(xhr.responseText); } else { // No, reject the promise reject(xhr); } } }; xhr.open("GET", path); xhr.send(); }); } // Do the request doGET("/path/to/file") .then(function(fileData) { // Use the file data }) .catch(function(xhr) { // The call failed, look at `xhr` for details }); 

Here in 2019, there's no reason to use XHR wrapped in a promise like that, just use fetch : 在2019年,没有理由将XHR包含在这样的承诺中,只需使用fetch

function doGET(url) {
    return fetch(url).then(response => {
        if (!response.ok) {
            throw new Error("HTTP error " + response.status); // Rejects the promise
        }
    });
}

Since you want to handle the local file, Try this 由于您要处理本地文件,请尝试此操作

Make use of XMLHttpRequest 利用XMLHttpRequest

function readFile(file)
{
    var f = new XMLHttpRequest();
    f.open("GET", file, false);
    f.onreadystatechange = function ()
    {
        if(f.readyState === 4)
        {
            if(f.status === 200 || f.status == 0)
            {
                var res= f.responseText;
                alert(res);
            }
        }
    }
    f.send(null);
}

Then you have to call with File:\\\\ 然后你必须使用File:\\\\调用File:\\\\

readFile('File:\\\yourpath');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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