Node.js - 生成multipart / form-data

时间:2011-10-30 15:50:07

标签: node.js multipartform-data

是否有Node.js库用于以下列形式生成多部分/表单数据内容?

------------------------------7a9cd2dc11c1
Content-Disposition: form-data; name="to"

destination@email.com
------------------------------7a9cd2dc11c1
Content-Disposition: form-data; name="from"

recipient@email.com
------------------------------7a9cd2dc11c1
Content-Disposition: form-data; name="subject"

subject line
------------------------------7a9cd2dc11c1
Content-Disposition: form-data; name="text"

This content does not matter.
------------------------------7a9cd2dc11c1--

3 个答案:

答案 0 :(得分:2)

fermata看起来可能是您正在寻找的东西。来自github页面:

fermata.json("http://example.com/some/action").post({
  'Content-Type':"multipart/form-data"
}, {
  fileField: form.input.file || {data:nodeBuffer, name:"", type:""}
}, callback)

答案 1 :(得分:0)

request支持multipart/form-data或者如果您只想创建内容正文,请尝试form-data

答案 2 :(得分:0)

我知道这是旧的,但我最近需要生成一个表单的多部分/表单数据主体字符串并遇到了这个线程。

我找不到任何以简单方式完成此操作的模块,因此我制作了自己的模块:https://github.com/kodie/form-data-body

你可以这样使用它:

const formDataBody = require('form-data-body')

// Specify form fields
const fields = {
  name: 'My test post',
  description: 'This is just a test post',
  items: ['First Item', 'Second Item'],

  // Files should be an object with the name, type, and data set to strings
  image: {
    name: 'hello.jpg',
    type: 'image/jpeg',
    data: binaryImageData
  }
}

const boundary = formDataBody.generateBoundary()
const header = {
  'Content-Type': `multipart/form-data; boundary=${boundary}`
}
const body = formDataBody(fields, boundary)

示例输出:

----------------------------071517909670537006900435
Content-Disposition: form-data; name="name"

My test post
----------------------------071517909670537006900435
Content-Disposition: form-data; name="description"

This is just a test post
----------------------------071517909670537006900435
Content-Disposition: form-data; name="items[]"

First Item
----------------------------071517909670537006900435
Content-Disposition: form-data; name="items[]"

Second Item
----------------------------071517909670537006900435
Content-Disposition: form-data; name="image"; filename="hello.jpg"
Content-Type: image/jpeg

[BINARY IMAGE DATA]
----------------------------071517909670537006900435--