【问题标题】:nodeJS file upload server does not change linenodeJS文件上传服务器不换行
【发布时间】:2019-05-04 14:34:46
【问题描述】:

我有一个非常简单的文件上传器脚本,我想在其中将一些日志附加到我的 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 和随机文件对其进行调试。我使用的命令是curl -X POST -d @filename 127.0.0.1:8080/logs/upload/Accelerometer.csv

文件的内容是:

0,0,0

1,2,3

2,4,7

但是在服务器端我得到了这个:

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

【问题讨论】:

    标签: node.js file-upload


    【解决方案1】:

    这仅仅是因为curl 从使用-d 选项发布的数据中删除了linebreaks。请改用--data-binary 选项。

    这里有更多关于 SO answer 发送带有 curl 的 newline 的信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-12
      • 2017-01-21
      • 1970-01-01
      • 2017-04-05
      • 2016-01-28
      • 2015-05-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多