jeudi 6 octobre 2016

How to send and receive multipart data in node.js?

Here is my HTML page where I want to send an image file and information about a TODO task and description. http://ift.tt/2duFhVR

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

I know how to receive the file using multiparty in node.js as shown above but I cannot for the life of me receive the information about the description and name of a TODO task.

Can anybody tell me the way to do it or point out some stupid mistake I'm making?




Aucun commentaire:

Enregistrer un commentaire