无法将数据写入Node JS中的文件 [英] Not able to write data to a file in Node JS
问题描述
我想在Node Js的已经创建的文件中写文本。这是我所做的事情
Upload.html
< / p> form enctype =multipart / form-dataaction =/ filemethod =post>
MAC地址:< br>
< input type =textname =macaddid =macadd>< br>
百分比:< br>
< input type =textname =percentid =percent>< br>
< input type =submitvalue ='Submit'id =upload>
< br>
< / form>
App.js
($)$
var macadd = req.body.macadd;
// var percent = req.body.percent;
var path =C:\Proj\doc\data.txt;
var data =hello;
fs.writeFile(path,macadd,function(error){
if(error){
console.error(write error:+ error.message);
} else {
console.log(成功写入+路径);
}
});
});
我想将'macadd'的值写入文件data.txt。它大多是一个整数值,但是当我点击提交时,在文件中,即使它显示为在控制台上成功写入,它也是未定义。任何解决方案?
我刚刚在路径的文件夹之间添加了两个斜线,并添加了路径解析包。就像这样:
$ b $ var path = resolve(C:\\Proj\\doc\\data.txt );
为了将文本框的值存入文件中,我必须删除form标签中的加密类型。 p>
< form action =/ filemethod =post>
MAC地址:< br>
< input type =textname =macaddid =macadd>< br>
百分比:< br>
< input type =textname =percentid =percent>< br>
< input type =submitvalue ='Submit'id =upload>
< br>
< / form>
I want to write text in an already created file in Node Js. Here is what I have done
Upload.html
<form enctype ="multipart/form-data" action ="/file" method ="post">
MAC Address:<br>
<input type="text" name="macadd" id="macadd"><br>
Percentage:<br>
<input type="text" name="percent" id="percent"><br>
<input type="submit" value='Submit' id="upload">
<br>
</form>
App.js
app.route('/file').post(function (req,res,next) {
var macadd =req.body.macadd;
//var percent =req.body.percent;
var path ="C:\Proj\doc\data.txt";
var data ="hello";
fs.writeFile(path,macadd , function(error) {
if (error) {
console.error("write error: " + error.message);
} else {
console.log("Successful Write to " + path);
}
});
});
I want to write the value of 'macadd' into the file data.txt. It is mostly an integer value but when I click on submit, in the file it comes as 'undefined' even though it shows as successful write on the console. Any solutions?
I just added 2 slashes between the folders of the path and added the path-resolve package. Something like this:
var path =resolve("C:\\Proj\\doc\\data.txt");
For getting the values of the text box into the file I had to remove the encryption type in the form tag.
<form action ="/file" method ="post">
MAC Address:<br>
<input type="text" name="macadd" id="macadd"><br>
Percentage:<br>
<input type="text" name="percent" id="percent"><br>
<input type="submit" value='Submit' id="upload">
<br>
</form>
这篇关于无法将数据写入Node JS中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!