2017-06-16 348 views
0

我想将数据保存到本地文件系统中的文件。在Nodejs中使用fs将数据保存到文件系统中

我想要实现的操作是当按钮发生点击时数据应该传递到后端并使用fs保存数据。

我有一个工作正常的FS代码。

var fs = require("fs"); 
var path = "D:\\Temp\\Test.txt"; 
var data = "Hello"; 

fs.writeFile(path, data, function(error) { 
if (error) { 
    console.error("write error: " + error.message); 
} else { 
    console.log("Successful Write to " + path); 
} 
}); 

然后我使用上面的fs代码在我的开发中,如下所示。

角前端控制器

$scope.sendToBack = function() 
{ 
    var csvData = { 
        dat: str 
        };   

     Event.postDataToCepFs(csvData) 
      .success(function() { 
       $scope.status = 'Reading the selected file'; 

      }) 
      .error(function (error) { 
       $scope.status = 'Unable to insert data: ' ; 
      }); 


    return str; 

    }; 

前端服务层

postDataToCepFs : function(event) { 
     return $http.post('/api/sendDataToCepFs', event); 
     $window.location.reload(); 
    }, 

后端路线

app.post('/api/sendDataToCepFs', function(req, res) { 

     var path = "D:\\Test.txt";   

      fs.writeFile(path, res.body.dat, function(error) { 
       if (error) { 
       console.error("write error: " + error.message); 
       } else { 
       console.log("Successful Write to " + path); 
       } 
      });   
    }); 

以上是我approach.but它给出“未能加载资源:服务器响应状态为500(内部服务器错误)

任何人都可以帮我这个吗?

谢谢

+0

在node.js中的控制台中的任何错误? –

+1

'res.body.dat'的类型是什么? –

+0

是的。我没有指定res.body.dat.now类型的固定。感谢@Omri Luzon – MSKP

回答

相关问题