|
| 1 | +--- |
| 2 | +title: 'Multipart 实体请求' |
| 3 | +prev_title: '请求体编码' |
| 4 | +prev_link: '/zh/docs/urlencoded' |
| 5 | +next_title: '注意事项' |
| 6 | +next_link: '/zh/docs/notes' |
| 7 | +--- |
| 8 | + |
| 9 | +## 使用 `multipart/form-data` 类型发起 `POST` 请求 |
| 10 | + |
| 11 | +### 使用 FormData API |
| 12 | + |
| 13 | +#### 浏览器 |
| 14 | + |
| 15 | +```js |
| 16 | +const form = new FormData(); |
| 17 | +form.append('my_field', 'my value'); |
| 18 | +form.append('my_buffer', new Blob([1,2,3])); |
| 19 | +form.append('my_file', fileInput.files[0]); |
| 20 | + |
| 21 | +axios.post('https://example.com', form) |
| 22 | +``` |
| 23 | + |
| 24 | +Axios 会将传入数据序列化,因此使用 Axios 提供的 API 可以无需手动处理 FormData 的数据并实现一样的效果: |
| 25 | + |
| 26 | +```js |
| 27 | +axios.postForm('https://httpbin.org/post', { |
| 28 | + my_field: 'my value', |
| 29 | + my_buffer: new Blob([1,2,3]), |
| 30 | + my_file: fileInput.files // FileList will be unwrapped as sepate fields |
| 31 | +}); |
| 32 | +``` |
| 33 | + |
| 34 | +HTML 表单可以直接作为请求内容来进行传输。 |
| 35 | + |
| 36 | +#### Node.js |
| 37 | + |
| 38 | +```js |
| 39 | +import axios from 'axios'; |
| 40 | + |
| 41 | +const form = new FormData(); |
| 42 | +form.append('my_field', 'my value'); |
| 43 | +form.append('my_buffer', new Blob(['some content'])); |
| 44 | + |
| 45 | +axios.post('https://example.com', form) |
| 46 | +``` |
| 47 | + |
| 48 | +由于 node.js 当前不支持从文件创建 `Blob`,因此您可以使用第三方软件包来实现该目的。 |
| 49 | + |
| 50 | +```js |
| 51 | +import {fileFromPath} from 'formdata-node/file-from-path' |
| 52 | + |
| 53 | +form.append('my_field', 'my value'); |
| 54 | +form.append('my_file', await fileFromPath('/foo/bar.jpg')); |
| 55 | + |
| 56 | +axios.post('https://example.com', form) |
| 57 | +``` |
| 58 | + |
| 59 | +当 Axios 版本小于 `v1.3.0` 时您必须引入 `form-data` 包。 |
| 60 | + |
| 61 | +```js |
| 62 | +const FormData = require('form-data'); |
| 63 | + |
| 64 | +const form = new FormData(); |
| 65 | +form.append('my_field', 'my value'); |
| 66 | +form.append('my_buffer', new Buffer(10)); |
| 67 | +form.append('my_file', fs.createReadStream('/foo/bar.jpg')); |
| 68 | + |
| 69 | +axios.post('https://example.com', form) |
| 70 | +``` |
| 71 | + |
| 72 | +### 🆕 自动序列化 |
| 73 | + |
| 74 | +从 `v0.27.0` 版本开始,当请求头中的 Content-Type 是 `multipart/form-data` 时,Axios 支持自动地将普通对象序列化成一个 FormData 对象。 |
| 75 | + |
| 76 | +这个示例请求演示了如何将一个数据通过 `FormData` 格式进行提交(浏览器与 Node.js 环境): |
| 77 | + |
| 78 | +```js |
| 79 | +import axios from 'axios'; |
| 80 | + |
| 81 | +axios.post('https://httpbin.org/post', { |
| 82 | + user: { |
| 83 | + name: 'Dmitriy' |
| 84 | + }, |
| 85 | + file: fs.createReadStream('/foo/bar.jpg') |
| 86 | +}, { |
| 87 | + headers: { |
| 88 | + 'Content-Type': 'multipart/form-data' |
| 89 | + } |
| 90 | +}).then(({data})=> console.log(data)); |
| 91 | +``` |
| 92 | + |
| 93 | +Axios FormData 序列化器支持一些特殊的结尾,以执行以下操作: |
| 94 | + |
| 95 | +- `{}` - 通过 `JSON.stringify` 序列化数据 |
| 96 | +- `[]` - 将 array-like 的对象使用相同的键值来展开为单独的字段 |
| 97 | + |
| 98 | +> 提示:默认情况下,展开、扩展操作将在数组和 FileList 对象上使用。 |
| 99 | +> |
| 100 | +
|
| 101 | +FormData 序列化器支持通过 `config.formSerializer: object` 这个参数来传递一些额外的选项,以支持一些特殊的情况: |
| 102 | + |
| 103 | +- `visitor: Function` - 用户定义的处理函数,将递归调用以按照自定义规则将数据对象序列化为`FormData`对象。 |
| 104 | +- `dots: boolean = false` - 使用点符号而不是括号来序列化数组和对象; |
| 105 | +- `metaTokens: boolean = true` - 在 FormData 键值中添加特殊结尾(例如`user{}: '{"name": "John"}'`)。后端的 body-parser 可能会使用此元信息自动将值解析为 JSON。 |
| 106 | +- `indexes: null|false|true = false` - 控制如何添加索引到打平的 array-like 对象的展开键值中 |
| 107 | + - `null` - 不添加中括号(`arr: 1`,`arr: 2`,`arr: 3`) |
| 108 | + - `false`(默认值)- 添加空中括号(`arr[]: 1`,`arr[]: 2`,`arr[]: 3`) |
| 109 | + - `true` - 添加带有索引的中括号(`arr[0]: 1`,`arr[1]: 2`,`arr[2]: 3`) |
| 110 | + |
| 111 | +假设说我们有一个这样的示例对象: |
| 112 | + |
| 113 | +```js |
| 114 | +const obj = { |
| 115 | + x: 1, |
| 116 | + arr: [1, 2, 3], |
| 117 | + arr2: [1, [2], 3], |
| 118 | + users: [{name: 'Peter', surname: 'Griffin'}, {name: 'Thomas', surname: 'Anderson'}], |
| 119 | + 'obj2{}': [{x:1}] |
| 120 | +}; |
| 121 | +``` |
| 122 | + |
| 123 | +接下来这些序列化的步骤将会由 Axios 内置的序列化器自动执行: |
| 124 | + |
| 125 | +```js |
| 126 | +const formData= new FormData(); |
| 127 | +formData.append('x', '1'); |
| 128 | +formData.append('arr[]', '1'); |
| 129 | +formData.append('arr[]', '2'); |
| 130 | +formData.append('arr[]', '3'); |
| 131 | +formData.append('arr2[0]', '1'); |
| 132 | +formData.append('arr2[1][0]', '2'); |
| 133 | +formData.append('arr2[2]', '3'); |
| 134 | +formData.append('users[0][name]', 'Peter'); |
| 135 | +formData.append('users[0][surname]', 'Griffin'); |
| 136 | +formData.append('users[1][name]', 'Thomas'); |
| 137 | +formData.append('users[1][surname]', 'Anderson'); |
| 138 | +formData.append('obj2{}', '[{"x":1}]'); |
| 139 | +``` |
| 140 | + |
| 141 | +```js |
| 142 | +import axios from 'axios'; |
| 143 | + |
| 144 | +axios.post('https://httpbin.org/post', { |
| 145 | + 'myObj{}': {x: 1, s: "foo"}, |
| 146 | + 'files[]': document.querySelector('#fileInput').files |
| 147 | +}, { |
| 148 | + headers: { |
| 149 | + 'Content-Type': 'multipart/form-data' |
| 150 | + } |
| 151 | +}).then(({data})=> console.log(data)); |
| 152 | +``` |
| 153 | + |
| 154 | +Axios支持以下别名方法:`postForm`,`putForm`,`patchForm`,这些方法只是对应的 HTTP 方法,其 content-type 头部默认设为`multipart/form-data`。 |
| 155 | + |
| 156 | +`FileList` 对象可以被直接传递: |
| 157 | + |
| 158 | +```js |
| 159 | +await axios.postForm('https://httpbin.org/post', document.querySelector('#fileInput').files) |
| 160 | +``` |
| 161 | + |
| 162 | +所有文件将使用相同的字段名`files[]`发送。 |
0 commit comments