Angularjs 在Nodejs中使用fs将数据保存到文件系统中的文件
我想将数据保存到本地文件系统中的文件中 我想要实现的操作是,当单击按钮时,数据应该传递到后端,并使用fs保存数据 我有一个fs代码,它工作得很好Angularjs 在Nodejs中使用fs将数据保存到文件系统中的文件,angularjs,node.js,fs,Angularjs,Node.js,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: " + er
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);
}
});
});
以上是我的方法。但它给出了“加载资源失败:服务器响应状态为500(内部服务器错误)”
谁能帮我这个吗
谢谢您在使用中间件吗? 穆特会很好的。在下面找到multer文档和示例的链接
您是否使用了中间件? 穆特会很好的。在下面找到multer文档和示例的链接
node.js的控制台中有错误吗?res.body.dat的类型是什么。我没有指定res.body.dat的类型。现在已经修复了。谢谢@Omri LuzonAny node.js控制台中的错误?res.body.dat的类型是什么??是的。我没有指定res.body.dat的类型。现在已修复。谢谢@Omri luzontanks@M.Navy!已经修好了。谢谢@M.Navy!已经修好了。