【问题标题】:Node.js how to fs.close from angular $http callNode.js 如何从角度 $http 调用 fs.close
【发布时间】:2018-03-03 03:17:01
【问题描述】:

我一直在努力开始和停止文件读取。我的 Angular html 页面中有两个按钮。一个用于开始,一个用于停止文件读取过程。文件读取代码在开始按钮上工作,但停止按钮不工作。不知道如何做到这一点。请给我一些建议。

<button id="starttodb" style="margin-left:25px;margin-right:25px;" ng-click="starttodb()">starttodb</button>
<button id="stoptodb" style="margin-left:25px;margin-right:25px;" ng-click="stoptodb()">stoptodb</button>

并且有两个功能与控制器中的这两个按钮相关联。

$scope.starttodb = function() {
            var filename = 'D:\\test.txt'
                $http({
                    url: '/starttodb',
                    method: "POST",
                    data: {filename: filename}                  
                    }).success(function (response) {
                                console.log("success in starttodb");                                
                    }); 
}; 
$scope.stoptodb = function() {
var filename = 'STOP'
    $http({
        url: '/starttodb',
        method: "POST",
        data: {filename: filename}
        }).success(function (response) {
                    console.log("success in starttodb");                                    
        });
};

在路由函数中调用这个文件读取代码。

var bite_size = 1024,readbytes = 0,file;
var filename = req.body.filename ;
var initcontent = fs.readFileSync(filename);
    console.log('Initial File content : \n' + initcontent);
    console.log('Initial File content length: \n' + initcontent.length);
    readbytes = initcontent.length; 
            fs.watchFile(filename, function() {         
                fs.open(filename, "r", function(error, fd) {               
                    fs.read(fd, new Buffer(bite_size), 0, bite_size, readbytes, function(err, bytecount, buff) {                                  
                      console.log(buff.toString('utf-8', 0, bytecount));
                      console.log('File Changed ...'+fd);
                      readbytes+=bytecount;
                    });             
                });
            })  

我想在点击停止按钮时停止读取此文件。

提前致谢。

【问题讨论】:

    标签: javascript jquery angularjs node.js


    【解决方案1】:

    要停止文件读取,您需要取消监视文件(如果您将有多个客户端,则通过特定的监听)

    您的启动函数将如下所示:

    // outside of the route
    var watchers = []  
    
    // inside the route
    var bite_size = 1024,readbytes = 0,file;
    var filename = req.body.filename ;
    var initcontent = fs.readFileSync(filename);
    console.log('Initial File content : \n' + initcontent);
    console.log('Initial File content length: \n' + initcontent.length);
    readbytes = initcontent.length;
    
    var watcherFunction = function() {         
        fs.open(filename, "r", function(error, fd) {               
            fs.read(fd, new Buffer(bite_size), 0, bite_size, readbytes, function(err, bytecount, buff) {                                  
                console.log(buff.toString('utf-8', 0, bytecount));
                console.log('File Changed ...'+fd);
                readbytes+=bytecount;
            });             
        });
    }
    
    watchers.push({
        client_id: some_client_id,
        watcherFunction: watcherFunction
    })
    
    fs.watchFile(filename, watcherFunction)  
    

    然后是您的停止文件(您将需要访问观察者变量,因此您需要将两个路由放在同一个文件中,或者将此变量放在单独的包中并将其导入两个路由文件)

    var clientWatcherIndex = watchers.findIndex(w => w.client_id === some_client_id)
    var clientWatcher = watchers[clientWatcherIndex]
    fs.unwatchFile(req.body.filename, clientWatcher.watcherFunction);
    watchers.splice(clientWatcherIndex , 1); // remove the watcher from list
    

    如果您只有一个客户端,则忽略 start 文件中的 watchers 变量,并在 stop 文件中使用:

    fs.unwatchFile(req.body.filename);
    

    【讨论】:

    • 非常感谢@Valera 的及时回复。我很感激。我会检查并很快接受答案。
    • 成功了。我将开始和停止路线放在同一个文件中,并将相同的文件名引用到停止路线。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-26
    • 2019-02-06
    • 1970-01-01
    • 1970-01-01
    • 2014-02-06
    • 1970-01-01
    • 2020-06-08
    相关资源
    最近更新 更多