From 6709011b1f357628a840ab409d9050260fecd906 Mon Sep 17 00:00:00 2001 From: Pu Wei Date: Wed, 3 Jul 2019 11:02:19 +0800 Subject: [PATCH] doc: upload file example code (#54) --- README.md | 8 ++++++-- README_zh-CN.md | 8 ++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index c506a9b..8408472 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,6 @@ The network request library, based on fetch encapsulation, combines the features | prefix | ✅ | ❎ | ❎ | | suffix | ✅ | ❎ | ❎ | | processing gbk | ✅ | ❎ | ❎ | -| quick Support | ✅ | ❓ | ❓ | For more discussion, refer to [Traditional Ajax is dead, Fetch eternal life](https://github.com/camsong/blog/issues/2) If you have good suggestions and needs, please mention [issue](https://github.com/umijs/umi/issues) @@ -134,9 +133,14 @@ request('/api/v1/some/api', { method:'post', requestType: 'form', data: {foo: 'b // reponseType: 'blob', how to handle the returned data, by default text and json are not added. Such as blob or formData need to add request('/api/v1/some/api', { reponseType: 'blob' }); -// Submit other data, such as text, upload files, etc., requestType is not filled, manually add the corresponding header. +// Submit other data, such as text, requestType is not filled, manually add the corresponding header. request('/api/v1/some/api', { method:'post', data: 'some data', headers: { 'Content-Type': 'multipart/form-data'} }); +// upload file +const formData = new FormData(); +formData.append('file', file); +request('/api/v1/some/api', { method:'post', data: formData }); + // The default is to return the data body, if you need the source response to expand, you can use the getResponse parameter. The result will be a set of layers request('/api/v1/some/api', { getResponse: true }).then({data, response} => {   console.log(data, response); diff --git a/README_zh-CN.md b/README_zh-CN.md index 92758ef..900096d 100644 --- a/README_zh-CN.md +++ b/README_zh-CN.md @@ -41,7 +41,6 @@ | 前缀 | ✅ | ❎ | ❎ | | 后缀 | ✅ | ❎ | ❎ | | 处理 gbk | ✅ | ❎ | ❎ | -| 快速支持 | ✅ | ❓ | ❓ | 更多讨论参考[传统 Ajax 已死,Fetch 永生](https://github.com/camsong/blog/issues/2), 如果你有好的建议和需求, 请提 [issue](https://github.com/umijs/umi/issues) @@ -138,9 +137,14 @@ request('/api/v1/some/api', { method:'post', requestType: 'form', data: {foo: 'b // reponseType: 'blob', 如何处理返回的数据, 默认情况下 text 和 json 都不用加. 如blob 或 formData 之类需要加 request('/api/v1/some/api', { reponseType: 'blob' }); -// 提交其他数据, 如文本, 上传文件等, requestType不填, 手动添加对应header. +// 提交其他数据, requestType不填, 手动添加对应header. request('/api/v1/some/api', { method:'post', data: 'some data', headers: { 'Content-Type': 'multipart/form-data'} }); +// 文件上传, 不要自己设置 Content-Type ! +const formData = new FormData(); +formData.append('file', file); +request('/api/v1/some/api', { method:'post', data: formData }); + // 默认返回的就是数据体, 如果需要源response来扩展, 可用getResponse参数. 返回结果会多套一层 request('/api/v1/some/api', { getResponse: true }).then({data, response} => { console.log(data, response);