【问题标题】:Cannot find all the data of my POSTed form?找不到我发布的表单的所有数据?
【发布时间】:2019-08-23 21:16:06
【问题描述】:

我正在将一个文件从我的网页上传到我的 nodejs 服务器,它已经可以工作了。该文件与其他一些输入值一起在一个表单中,我无法找到如何阅读。

HTML

<form class="modal-content" method="POST" action="http://localhost:3000/testupload"  enctype="multipart/form-data" >
    <span class="close">&times;</span>
    <div class="modal-body">
        <input type="file" value="Upload file" name="file" id="input-upload">
        <input type="checkbox" value="Automatic Transcription" name="TESTCHECK">
        <div>
            <input type="checkbox" value="Automatic subtitling">
            <select name="testSelect">
                <option value="en-us">US English</option>
            </select>
        </div>
    </div>
    <div class="modal-footer">
        <button >Upload Media</button>
    </div>
</form>

nodejs

app.post("/testupload", (request, response, next) =>{
    let form = new formidable.IncomingForm();
    form.parse(request, function(err, fields, files){
        //console.log(fields);
        console.log(files.file.name); //Correctly gets the filename
        //further file upload code

    });
    console.log(request);
    response.end();
});

当我发布表单时,我可以取回文件输入,但在请求中的任何地方都找不到其他输入和值。

【问题讨论】:

  • console.log(fields) 怎么样?
  • 您的其他输入没有名称属性的任何原因? (html)
  • 输入值现在在字段中,当我在更正代码之前对其进行测试时,它只返回了一个空的“{}”,所以这是正确的
  • @EvansM。只是让它变得非常快速和肮脏,在我的 POST 中得到一些东西之前还没有修复它,但是感谢你的捕获

标签: html node.js forms


【解决方案1】:

根据文档,您可以使用事件:

form.on('field', function(name, value) {
});

您也可以通过字段直接引用(如https://www.npmjs.com/package/formidable 上的示例)

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}));
    });

据我了解,您需要正确命名表单字段(首先尝试一个简单的示例,一个输入文本字段)。

【讨论】:

  • 输入值现在在字段中,当我在更正代码之前测试它时,它只返回一个空的'{}'并且没有再看它。谢谢!
猜你喜欢
  • 2013-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多