This repository was archived by the owner on Nov 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathformdata.dart
More file actions
120 lines (109 loc) · 2.74 KB
/
formdata.dart
File metadata and controls
120 lines (109 loc) · 2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import 'dart:convert';
import 'dart:io';
import 'package:dio_http/dio_http.dart';
import 'package:dio_http/adapter.dart';
void showProgress(received, total) {
if (total != -1) {
print((received / total * 100).toStringAsFixed(0) + '%');
}
}
Future<FormData> FormData1() async {
return FormData.fromMap({
'name': 'wendux',
'age': 25,
'file': await MultipartFile.fromFile(
'./example/xx.png',
filename: 'xx.png',
),
'files': [
await MultipartFile.fromFile(
'./example/upload.txt',
filename: 'upload.txt',
),
MultipartFile.fromFileSync(
'./example/upload.txt',
filename: 'upload.txt',
),
]
});
}
Future<FormData> FormData2() async {
var formData = FormData();
formData.fields
..add(MapEntry(
'name',
'wendux',
))
..add(MapEntry(
'age',
'25',
));
formData.files.add(MapEntry(
'file',
await MultipartFile.fromFile(
'./example/xx.png',
filename: 'xx.png',
),
));
formData.files.addAll([
MapEntry(
'files',
await MultipartFile.fromFile(
'./example/upload.txt',
filename: 'upload.txt',
),
),
MapEntry(
'files',
MultipartFile.fromFileSync(
'./example/upload.txt',
filename: 'upload.txt',
),
),
]);
return formData;
}
Future<FormData> FormData3() async {
return FormData.fromMap({
'file': await MultipartFile.fromFile(
'./example/upload.txt',
filename: 'uploadfile',
),
});
}
/// FormData will create readable "multipart/form-data" streams.
/// It can be used to submit forms and file uploads to http server.
void main() async {
var dio = Dio();
dio.options.baseUrl = 'http://localhost:3000/';
dio.interceptors.add(LogInterceptor());
//dio.interceptors.add(LogInterceptor(requestBody: true));
(dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate =
(HttpClient client) {
client.findProxy = (uri) {
//proxy all request to localhost:8888
return 'PROXY localhost:8888';
};
client.badCertificateCallback =
(X509Certificate cert, String host, int port) => true;
};
Response response;
var formData1 = await FormData1();
var formData2 = await FormData2();
var bytes1 = await formData1.readAsBytes();
var bytes2 = await formData2.readAsBytes();
assert(bytes1.length == bytes2.length);
var t = await FormData3();
print(utf8.decode(await t.readAsBytes()));
response = await dio.post(
//"/upload",
'http://localhost:3000/upload',
data: await FormData3(),
onSendProgress: (received, total) {
if (total != -1) {
print((received / total * 100).toStringAsFixed(0) + '%');
}
},
);
print(response);
}