【问题标题】:How to handle form data in an http requests in nodejs如何在nodejs中处理http请求中的表单数据
【发布时间】:2013-05-17 03:47:52
【问题描述】:

我正在编写一个代码,让客户端在服务器上上传两个文件。由于我使用了director路由器,所以我设置了一个这样的监听器:

request.chunks = [];
request.on('data', function (chunk) {
    request.chunks.push( chunk.toString());
};

这是客户端上传文件时块的console.log(基于浏览器,边界会发生变化):

-----------------------------7dd2c419180232
Content-Disposition: form-data; name="filename"


-----------------------------7dd2c419180232
Content-Disposition: form-data; name="uploadfile"; filename="first.txt"
Content-Type: application/octet-stream

the content of first file

-----------------------------7dd2c419180232
Content-Disposition: form-data; name="wfilename"


-----------------------------7dd2c419180232
Content-Disposition: form-data; name="wuploadfile"; filename="second.txt"
Content-Type: application/octet-stream

the content of the second file

-----------------------------7dd2c419180232--

我已经通过一些正则表达式处理了这个问题,用于提取 request.chunks 变量上的每个文件名和每个文件内容,但是浏览器有不同的倾向(对于这些边界,例如谷歌浏览器是这样的: '------WebKit...') 我想知道是否有一种直接的方法来解析文件名和文件内容(显然来自request.chunks 而不是request)与一些模块,如强大或多-part 还是查询字符串?


感谢@micnic,我想出了一个标题解析器。它可能需要在此级别受欢迎的修订:

exports.parseMultipart = function(request) {

    // Convert the chunks to string
    var str = request.chunks.toString();

    // Get the boundry out pf header
    var boundry = '--' + request.headers["content-type"].substring(request.headers["content-type"].indexOf('=')+1, request.headers["content-type"].length);

    // Initialization
    var request_data = {};
    index = 0;


    // For each form element, store the value in request_data
    while (str.indexOf(boundry, index) != -1) {
        index += boundry.length;
        i = str.indexOf(" name=\"",index);
        j = str.indexOf("\"",i+7);
        name = str.substring(i+7,j);
        var value = {};
        if (str.charAt(j+1)==';') {
            value["type"] = "file";
            i = j + 3;
            j = str.indexOf("\"",i+14);
            filename = str.substring(i+10, j);
            value["filename"] = filename;
            i = j + 17;
            j = str.indexOf("\r", i);
            contentType = str.substring(i, j);
            value["content-type"] = contentType;
            i = j + 4;
            j = str.indexOf("\n\r\n" + boundry, i);
            fileContent = str.substring(i, j);
            value["content"] = fileContent;
        } else {
            value["type"] = "field";
            i = j + 5;
            j = str.indexOf("\r\n" + boundry,i);
            value["content"] = str.substring(i,j);
        }
        index = str.indexOf(boundry, index) + 2;
        request_data[name] = value;
    }
    return request_data;
}

【问题讨论】:

  • 你说的厉害,那就用它吧;-)
  • @TheHippo formidable 使用请求正文,但我想使用块变量!

标签: node.js parsing file-upload multipartform-data formidable


【解决方案1】:

对于我的模块simpleS,我写了一个解析器: https://github.com/micnic/simpleS/blob/ccc8e600013da70d5204c1b66149e834d2c0fea2/utils/utils.js#L365

可能有点难以理解,但它确实有效。

"...我想知道是否有直接的方法来解析文件名和文件内容与一些模块,如强大的或多部分或查询字符串?...",你读过 formidable 的文档吗?

从那里:

var formidable = require('formidable');

/* ... */

var form = new formidable.IncomingForm();

form.parse(req, function(err, fields, files) {
  res.writeHead(200, {'content-type': 'text/plain'});
  res.write('received upload:\n\n');
  res.end(util.inspect({fields: fields, files: files}));
});

/* ... */

【讨论】:

  • 感谢第一个解决方案,但第二个解决方案与我的数据侦听器后为空的 'req' 一起使用!我想要一个获取“req.chunks”并解析字段和文件的模块。
  • 非常感谢您的代码,我编写了另一个代码来将大表单解析为 JSON 对象,如果您发现它在 SimpleS 中有用,我可以与您分享
  • 我想看看,也许我可以帮助改进它
  • 我在帖子中添加了代码,非常感谢您的评论
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-03
  • 2018-06-23
  • 2018-12-07
  • 2013-07-27
  • 2017-04-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多