2017-09-21 32 views
0

我一直在努力奮鬥多天來啓動和停止文件讀取。我在我的角度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; 
        });    
       }); 
      }) 

我想停止此文件閱讀停止按鈕單擊。

在此先感謝。

回答

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 

如果你只有一個客戶端,然後忽略啓動文件中的觀察家變量,在停止使用文件:

fs.unwatchFile(req.body.filename); 
+0

非常感謝@Valera即期生存。我很感激。我會盡快覈實並接受答案。 – sand

+0

它解決了嗎? – Valera

+0

它工作。我將啓動和停止路由放在同一個文件中,並且引用相同的文件名來停止路由。 – sand