Skip to content

Commit

Permalink
update doc
Browse files Browse the repository at this point in the history
  • Loading branch information
wendux committed Sep 19, 2019
1 parent 7e13cfb commit e288595
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 37 deletions.
35 changes: 35 additions & 0 deletions README-ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,41 @@ response = await dio.post("/info", data: formData);
这里有一个完整的[示例](https://github.com/flutterchina/dio/blob/master/example/formdata.dart).

### 多文件上传

多文件上传时,通过给key加中括号“[]”方式作为文件数组的标记,大多数后台也会通过key[]这种方式来读取。不过RFC中并没有规定多文件上传就必须得加“[]”,所以有时不带“[]”也是可以的,关键在于后台和客户端得一致。v3.0.0 以后通过`Formdata.fromMap()`创建的`Formdata`,如果有文件数组,是默认会给key加上“[]”的,比如:

```dart
FormData.fromMap({
"files": [
MultipartFile.fromFileSync("./example/upload.txt",
filename: "upload.txt"),
MultipartFile.fromFileSync("./example/upload.txt",
filename: "upload.txt"),
]
});
```

最终编码时会key会为 "files[]",**如果不想添加“[]**,可以通过`Formdata`的API来构建:

```dart
var formData = FormData();
formData.files.addAll([
MapEntry(
"files",
MultipartFile.fromFileSync("./example/upload.txt",
filename: "upload.txt"),
),
MapEntry(
"files",
MultipartFile.fromFileSync("./example/upload.txt",
filename: "upload.txt"),
),
]);
```

这样构建的`FormData`的key是不会有“[]”。

## 转换器

转换器`Transformer` 用于对请求数据和响应数据进行编解码处理。Dio实现了一个默认转换器`DefaultTransformer`作为默认的 `Transformer`. 如果你想对请求/响应数据进行自定义编解码处理,可以提供自定义转换器,通过 `dio.transformer`设置。
Expand Down
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,39 @@ response = await dio.post("/info", data: formData);
There is a complete example [here](https://github.com/flutterchina/dio/blob/master/example/formdata.dart).

### Multiple files upload

There are two ways to add multiple files to ` FormData`, the only difference is that upload keys are different for array types。

```dart
FormData.fromMap({
"files": [
MultipartFile.fromFileSync("./example/upload.txt",
filename: "upload.txt"),
MultipartFile.fromFileSync("./example/upload.txt",
filename: "upload.txt"),
]
});
```

The upload key eventually becomes "files[]",This is because many back-end services add a middle bracket to key when they get an array of files. **If you don't want “[]**,you should create FormData as follows(Don't use `FormData.fromMap`):

```dart
var formData = FormData();
formData.files.addAll([
MapEntry(
"files",
MultipartFile.fromFileSync("./example/upload.txt",
filename: "upload.txt"),
),
MapEntry(
"files",
MultipartFile.fromFileSync("./example/upload.txt",
filename: "upload.txt"),
),
]);
```

## Transformer

`Transformer` allows changes to the request/response data before it is sent/received to/from the server. This is only applicable for request methods 'PUT', 'POST', and 'PATCH'. Dio has already implemented a `DefaultTransformer`, and as the default `Transformer`. If you want to customize the transformation of request/response data, you can provide a `Transformer` by your self, and replace the `DefaultTransformer` by setting the `dio.transformer`.
Expand Down
35 changes: 35 additions & 0 deletions dio/README-ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,41 @@ response = await dio.post("/info", data: formData);
这里有一个完整的[示例](https://github.com/flutterchina/dio/blob/master/example/formdata.dart).

### 多文件上传

多文件上传时,通过给key加中括号“[]”方式作为文件数组的标记,大多数后台也会通过key[]这种方式来读取。不过RFC中并没有规定多文件上传就必须得加“[]”,所以有时不带“[]”也是可以的,关键在于后台和客户端得一致。v3.0.0 以后通过`Formdata.fromMap()`创建的`Formdata`,如果有文件数组,是默认会给key加上“[]”的,比如:

```dart
FormData.fromMap({
"files": [
MultipartFile.fromFileSync("./example/upload.txt",
filename: "upload.txt"),
MultipartFile.fromFileSync("./example/upload.txt",
filename: "upload.txt"),
]
});
```

最终编码时会key会为 "files[]",**如果不想添加“[]**,可以通过`Formdata`的API来构建:

```dart
var formData = FormData();
formData.files.addAll([
MapEntry(
"files",
MultipartFile.fromFileSync("./example/upload.txt",
filename: "upload.txt"),
),
MapEntry(
"files",
MultipartFile.fromFileSync("./example/upload.txt",
filename: "upload.txt"),
),
]);
```

这样构建的`FormData`的key是不会有“[]”。

## 转换器

转换器`Transformer` 用于对请求数据和响应数据进行编解码处理。Dio实现了一个默认转换器`DefaultTransformer`作为默认的 `Transformer`. 如果你想对请求/响应数据进行自定义编解码处理,可以提供自定义转换器,通过 `dio.transformer`设置。
Expand Down
34 changes: 34 additions & 0 deletions dio/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,39 @@ response = await dio.post("/info", data: formData);
There is a complete example [here](https://github.com/flutterchina/dio/blob/master/example/formdata.dart).

### Multiple files upload

There are two ways to add multiple files to ` FormData`, the only difference is that upload keys are different for array types。

```dart
FormData.fromMap({
"files": [
MultipartFile.fromFileSync("./example/upload.txt",
filename: "upload.txt"),
MultipartFile.fromFileSync("./example/upload.txt",
filename: "upload.txt"),
]
});
```

The upload key eventually becomes "files[]",This is because many back-end services add a middle bracket to key when they get an array of files. **If you don't want “[]**,you should create FormData as follows(Don't use `FormData.fromMap`):

```dart
var formData = FormData();
formData.files.addAll([
MapEntry(
"files",
MultipartFile.fromFileSync("./example/upload.txt",
filename: "upload.txt"),
),
MapEntry(
"files",
MultipartFile.fromFileSync("./example/upload.txt",
filename: "upload.txt"),
),
]);
```

## Transformer

`Transformer` allows changes to the request/response data before it is sent/received to/from the server. This is only applicable for request methods 'PUT', 'POST', and 'PATCH'. Dio has already implemented a `DefaultTransformer`, and as the default `Transformer`. If you want to customize the transformation of request/response data, you can provide a `Transformer` by your self, and replace the `DefaultTransformer` by setting the `dio.transformer`.
Expand Down Expand Up @@ -674,3 +707,4 @@ Please file feature requests and bugs at the [issue tracker][tracker].
Buy a cup of coffee for me (Scan by wechat):

![](https://cdn.jsdelivr.net/gh/flutterchina/[email protected]/docs/imgs/pay.jpeg)

40 changes: 3 additions & 37 deletions example/test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,48 +4,14 @@ import 'package:dio/adapter.dart';
import 'package:dio/dio.dart';

void getHttp() async {
var file=File("./example/log.txt");
var sink=file.openWrite();
try {
var dio = Dio();
dio.interceptors.add(LogInterceptor(logPrint: sink.writeln));
(dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate =
(HttpClient client) {
client.findProxy = (uri) {
//proxy all request to localhost:8888
return "PROXY localhost:8888";
};
};
Response response = await dio.post(
"http://httpbin.org/post",
data: {"rows": 1, "page": 10},
);
//Response response = await dio.get("http://xxx.xxx");
print(response);
Response response = await Dio().get("http://www.google.com");
print(response.data);
} catch (e) {
print(e);
}
await sink.close();
}

main() async {
var data = {
"ownerId": "ccccc",
"token":"cvvvvvvvv",
"userName": "dffffffff",
"subtime": "2019-09-01"
};

Dio dio = new Dio();
BaseOptions options = new BaseOptions(
baseUrl: "xxxxxxx",
connectTimeout: 50000,
);
dio.options = options;
await dio.post('http://httpbin.org/post', data: data).then((value) {
print('value:' + value.toString());
}).catchError((e) {
print('e >> $e');
});
//await getHttp();
await getHttp();
}

0 comments on commit e288595

Please sign in to comment.