节点js获取值字段类型,形式为enctype multiform

时间:2016-03-09 13:05:04

标签: node.js html-form

我正在使用以下代码:

的node.js

app.post('/upload_file', function(req, res) {
    var fstream;
    req.pipe(req.busboy);
    var image_name=req.body.image_name;

    req.busboy.on('file', function (fieldname, file, filename) {
        var file_name=randomstring.generate(7)+"_"+filename;
        fstream = fs.createWriteStream(__dirname + '/uploads/' + file_name);
        file.pipe(fstream);
        fstream.on('close', function () {
           // res.send('uploaded Successfully');

        });

    });
    res.send(image_name+""+file_name);
});

HTML

<form action="/upload_file" method="POST" enctype="multipart/form-data">
  Select an image to upload:
  <input type="file" name="image"> 
  <input type="text" value="test" name="image_name">
  <input type="submit" value="Upload Image">
</form>

文件已成功上传,但我无法提取字段image_name, 它在输出中给出了undefined

2 个答案:

答案 0 :(得分:0)

默认情况下,节点似乎没有解析multipart / form-data,因此你的字段image_name是未定义的。

很好,您正在使用busboy,但捕获image_name的唯一方法是使用以下文件输入:

req.busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
        if(fieldname === 'image_name') {
             image_name = fieldname;
         }
});

对于fieldname,您可能需要以下内容:

req.busboy.on('field', function(fieldname, val, fieldnameTruncated, valTruncated, encoding, mimetype) {
      if(fieldname === 'image_name') {
                 image_name = fieldname;
             }
    });

答案 1 :(得分:0)

经过各种实验,我得到了以下解决方案

uniqReference = NSUUID().UUIDString
let singleLink2LinkthemALL = CKRecordID(recordName: uniqReference)
let theLinkRef = CKReference(recordID: singleLink2LinkthemALL, action: .DeleteSelf)
newRecord2.setObject(theLinkRef, forKey: "theLink")