代码之家  ›  专栏  ›  技术社区  ›  George Sp

nodeJS文件上载服务器未更改行

  •  0
  • George Sp  · 技术社区  · 5 年前

    我有一个非常简单的文件上传器脚本,其中我想将一些日志附加到我的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

    但在服务器端,我得到以下信息:

    enter image description here

    如何使服务器端文件具有与原始文件相同的格式?

    0 回复  |  直到 5 年前
        1
  •  1
  •   1565986223    5 年前

    这只是因为 curl 除去 linebreaks 从与 -d 选项使用 --data-binary 取而代之的是选择。

    还有更多 SO answer newline 卷曲的。