【问题标题】:How to send and receive multipart data in node.js?如何在 node.js 中发送和接收多部分数据?
【发布时间】:2016-10-08 19:07:34
【问题描述】:

这是我的 HTML 页面,我想在其中发送图像文件以及有关 TODO 任务和描述的信息。 http://pastebin.com/W9TVy4An

form.on('part', (part) => {
if (part.filename) {
  let imagePath = ''
  let privateIndex  // will hold the index for private images
  let index  // will hold the index for the public images
  let file = ''

  part.setEncoding('binary')
  part.on('data', (data) => {
    file += data
  })
  part.on('end', () => {

    if (isPrivate) {
      // handle a private image
    } else {
      // handle a public image

    }
    if (!fs.existsSync(imagePath)) {
      fs.mkdir(imagePath)  // create the folder for the image
    }

    imagePath += isPrivate ? privateIndex + '.jpg' : index + '.jpg'  // add the image name to complete the path for saving

    fs.writeFile(imagePath, file, 'ascii', (err) => {
      // stuff
      }
    })

    callback(part.filename)
  })

我知道如何使用 node.js 中的多方接收文件,如上所示,但我无法终生接收有关 TODO 任务的描述和名称的信息。

谁能告诉我怎么做或指出我犯的一些愚蠢的错误?

【问题讨论】:

    标签: html node.js web webserver


    【解决方案1】:

    好的,我想出了一个办法,就是在解析表单时读取fieldsfiles 属性。

    let form = new multiparty.Form()
    form.parse(req, (err, fields, files) => {
      console.log(fields)
    })
    

    Fields 是一个以属性名作为键,以值作为值的对象。 结果是

    { todoname: [ 'Post the answer' ], tododesc: [ 'Post the answer you figured out on your StackOverflow question' ] }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-10
      • 1970-01-01
      • 1970-01-01
      • 2013-07-18
      相关资源
      最近更新 更多