Skip to content

Commit

Permalink
doc: upload file example code (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
clock157 authored Jul 3, 2019
1 parent 75ffc14 commit 6709011
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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);
Expand Down
8 changes: 6 additions & 2 deletions README_zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
| 前缀 ||||
| 后缀 ||||
| 处理 gbk ||||
| 快速支持 ||||

更多讨论参考[传统 Ajax 已死,Fetch 永生](https://github.com/camsong/blog/issues/2), 如果你有好的建议和需求, 请提 [issue](https://github.com/umijs/umi/issues)

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 6709011

Please sign in to comment.