我有一个非常简单的文件上传器脚本,其中我想将一些日志附加到我的nodejs服务器上。它成功地获取了文件的数据,但未能在服务器端更改行。
这是我目前的脚本:
var http = require('http');
var fs = require('fs');
http.createServer(function (req, res) {
if(req.method== 'POST'){
console.log('bhke sto POST');
if (req.url == '/logs/upload/Accelerometer.csv') {
console.log("Calledeixa to swsto path");
var body="";
req.on('data', data=>{
body+=data;
console.log("partial data ="+data);
fs.appendFile('Accelerometer.csv', body, function (err) {
console.log('Saved!');
});
});
console.log(body);
req.on('end', function(){
res.writeHead(200, {'Content-Type' : 'text/html'})
res.end('Accelerometer.csv was updated successfully\n');
});
} else {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write('<form action="fileupload" method="post" enctype="multipart/form-data">');
res.write('<input type="file" name="filetoupload"><br>');
res.write('<input type="submit">');
res.write('</form>');
return res.end();
}
}
}).listen(8080);
curl -X POST -d @filename 127.0.0.1:8080/logs/upload/Accelerometer.csv
0,0,0
2,4,7
但在服务器端,我得到以下信息:
如何使服务器端文件具有与原始文件相同的格式?