Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
N
netrain_flutter_app
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
邹志胜
netrain_flutter_app
Commits
05678ce9
Commit
05678ce9
authored
Jul 23, 2021
by
“Icebear”
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
添加多张图片上传解决办法
parent
d1cf6148
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
77 additions
and
38 deletions
+77
-38
IService.dart
...ain_flutter_app/lib/douwen/custom/http/base/IService.dart
+1
-0
LoginApi.dart
netrain_flutter_app/lib/zhangfeng/LoginApi.dart
+2
-3
LoginPage.dart
netrain_flutter_app/lib/zhangfeng/LoginPage.dart
+54
-24
HttpManager.dart
netrain_flutter_app/lib/zhangfeng/Network/HttpManager.dart
+20
-11
No files found.
netrain_flutter_app/lib/douwen/custom/http/base/IService.dart
View file @
05678ce9
...
...
@@ -39,6 +39,7 @@ mixin IService {
_analysisApiException
(
iEntity
);
}
else
{
iEntity
=
new
IEntity
(
code:
HttpCode
.
NORMAL_ERROR
,
msg:
e
.
toString
());
}
}
return
iEntity
;
...
...
netrain_flutter_app/lib/zhangfeng/LoginApi.dart
View file @
05678ce9
import
'Network/HttpManager.dart'
;
import
'Network/BaseHttpModel.dart'
;
import
'UserModel.dart'
;
/// 所有接口类
...
...
@@ -17,8 +16,8 @@ class LoginApi {
}
///上传头像
static
uploadAvatarRequest
(
String
avatarPath
,
OnResult
onResult
)
{
return
HttpManager
.
getInstance
().
request
(
'file/upload'
,
parameters:
null
,
method:
"POST"
,
image
Path:
avatarPath
,
onResult:
(
data
,
errorMsg
){
static
uploadAvatarRequest
(
List
images
,
OnResult
onResult
)
{
return
HttpManager
.
getInstance
().
request
(
'file/upload'
,
parameters:
null
,
method:
"POST"
,
image
s:
images
,
onResult:
(
data
,
errorMsg
){
if
(
onResult
!=
null
){
onResult
(
data
,
errorMsg
);
}
...
...
netrain_flutter_app/lib/zhangfeng/LoginPage.dart
View file @
05678ce9
...
...
@@ -20,7 +20,8 @@ class _LoginState extends State<LoginRequestPage> {
// This widget is the root of your application.
final
phoneTextField
=
TextEditingController
();
final
pwdTextField
=
TextEditingController
();
PickedFile
_image
;
PickedFile
_image1
;
PickedFile
_image2
;
@override
void
dispose
()
{
...
...
@@ -56,19 +57,29 @@ class _LoginState extends State<LoginRequestPage> {
Fluttertoast
.
showToast
(
msg:
'退出成功'
);
}
Future
getImage
(
)
async
{
void
_getImage
(
int
type
)
async
{
if
(
UserModel
.
getModel
()
==
null
){
Fluttertoast
.
showToast
(
msg:
'请登录'
);
return
;
}
var
image
=
await
ImagePicker
.
platform
.
pickImage
(
source
:
ImageSource
.
gallery
);
_upLoadImage
(
image
);
//上传图片
setState
(()
{
_image
=
image
;
if
(
type
==
0
){
_image1
=
image
;
}
else
{
_image2
=
image
;
}
});
}
_upLoadImage
(
PickedFile
image
)
async
{
LoginApi
.
uploadAvatarRequest
(
image
.
path
,
(
httpModel
,
error
){
_upLoadImage
()
{
List
images
=
new
List
();
if
(
_image1
!=
null
){
images
.
add
(
_image1
.
path
);
}
if
(
_image2
!=
null
){
images
.
add
(
_image2
.
path
);
}
LoginApi
.
uploadAvatarRequest
(
images
,
(
httpModel
,
error
){
if
(
error
!=
null
){
Fluttertoast
.
showToast
(
msg:
error
);
}
else
{
...
...
@@ -76,9 +87,10 @@ class _LoginState extends State<LoginRequestPage> {
}
});
}
void
_requestListData
()
{
if
(
UserModel
.
getModel
()
==
null
)
{
Fluttertoast
.
showToast
(
msg:
'请登录'
);
if
(
UserModel
.
getModel
()
==
null
)
{
Fluttertoast
.
showToast
(
msg:
'请登录'
);
return
;
}
Map
<
String
,
String
>
map
=
new
Map
<
String
,
String
>();
...
...
@@ -87,16 +99,18 @@ class _LoginState extends State<LoginRequestPage> {
if
(
error
!=
null
)
{
Fluttertoast
.
showToast
(
msg:
error
);
}
else
{
List
examList
=
httpModel
.
data
[
"content"
].
map
((
m
)
=>
new
ExamListModel
.
fromJson
(
m
)).
toList
();
if
(
examList
.
length
>
0
){
List
examList
=
httpModel
.
data
[
"content"
]
.
map
((
m
)
=>
new
ExamListModel
.
fromJson
(
m
))
.
toList
();
if
(
examList
.
length
>
0
)
{
ExamListModel
model
=
examList
[
0
];
Fluttertoast
.
showToast
(
msg:
model
.
toJson
().
toString
(),
timeInSecForIos:
5
);
Fluttertoast
.
showToast
(
msg:
model
.
toJson
().
toString
(),
timeInSecForIos:
5
);
}
}
});
}
@override
Widget
build
(
BuildContext
context
)
{
return
Scaffold
(
...
...
@@ -159,22 +173,38 @@ class _LoginState extends State<LoginRequestPage> {
),
Container
(
margin:
EdgeInsets
.
only
(
top:
50
),
width:
1
00
,
width:
3
00
,
height:
100
,
child:
ElevatedButton
(
child:
Text
(
"上传头像"
),
onPressed:
getImage
,
child:
Row
(
mainAxisAlignment:
MainAxisAlignment
.
start
,
children:
[
GestureDetector
(
child:
Image
.
asset
(
_image1
!=
null
?
_image1
.
path
:
Images
.
lake
),
onTap:
(){
_getImage
(
0
);
},
),
GestureDetector
(
child:
Image
.
asset
(
_image2
!=
null
?
_image2
.
path
:
Images
.
lake
),
onTap:
(){
_getImage
(
1
);
},
),
],
)
),
Container
(
margin:
EdgeInsets
.
only
(
top:
0
),
width:
100
,
height:
100
,
child:
Image
.
asset
(
_image
!=
null
?
_image
.
path
:
Images
.
lake
),
width:
150
,
height:
50
,
child:
ElevatedButton
(
child:
Text
(
"上传多张图"
),
onPressed:
_upLoadImage
,
),
),
Container
(
margin:
EdgeInsets
.
only
(
top:
10
),
width:
1
0
0
,
width:
1
5
0
,
height:
50
,
child:
ElevatedButton
(
child:
Text
(
"请求列表数据"
),
...
...
netrain_flutter_app/lib/zhangfeng/Network/HttpManager.dart
View file @
05678ce9
import
'dart:convert'
;
import
'dart:ffi'
;
import
'dart:io'
;
import
'package:dio/dio.dart'
;
import
'package:netrain_flutter_app/zhangfeng/Network/BaseHttpModel.dart'
;
import
'package:netrain_flutter_app/zhangfeng/UserModel.dart'
;
...
...
@@ -78,30 +79,38 @@ class HttpManager {
request
(
String
url
,
{
parameters
,
method
=
'GET'
,
OnResult
onResult
,
String
imagePath
})
async
{
OnResult
onResult
,
List
images
})
async
{
parameters
=
parameters
??
{};
if
(
onResult
==
null
){
return
;
}
try
{
FormData
formdata
;
if
(
imagePath
!=
null
){
if
(
images
!=
null
){
List
<
MultipartFile
>
imageList
=
new
List
<
MultipartFile
>();
for
(
String
imagePath
in
images
)
{
var
name
=
imagePath
.
substring
(
imagePath
.
lastIndexOf
(
"/"
)
+
1
,
imagePath
.
length
);
var
suffix
=
name
.
substring
(
name
.
lastIndexOf
(
"."
)
+
1
,
name
.
length
);
formdata
=
FormData
.
fromMap
({
"file"
:
await
MultipartFile
.
fromFile
(
imagePath
,
//图片路径
MultipartFile
multipartFile
=
await
MultipartFile
.
fromFile
(
imagePath
,
//这个字段要有,否则后端接收为null
filename:
name
,
)
);
imageList
.
add
(
multipartFile
);
}
formdata
=
new
FormData
.
fromMap
({
//后端要用multipartFiles接收参数,否则为null
"multipartFiles"
:
imageList
});
}
parameters
=
new
Map
<
String
,
dynamic
>.
from
(
parameters
);
Map
<
String
,
String
>
map
=
new
Map
<
String
,
String
>();
Map
<
String
,
String
>
headers
=
new
Map
<
String
,
String
>();
if
(
UserModel
.
getModel
()
!=
null
)
{
map
[
UserModel
.
getModel
().
header
]
=
UserModel
.
getModel
().
tokenHead
+
UserModel
.
getModel
().
token
;
headers
[
UserModel
.
getModel
().
header
]
=
UserModel
.
getModel
().
tokenHead
+
UserModel
.
getModel
().
token
;
}
Response
response
=
await
_dio
.
request
(
url
,
queryParameters:
parameters
,
data:
formdata
,
options:
new
Options
(
method:
method
,
headers:
map
,
responseType:
ResponseType
.
plain
));
queryParameters:
parameters
,
data:
formdata
,
options:
new
Options
(
method:
method
,
headers:
headers
,
responseType:
ResponseType
.
plain
));
Map
<
String
,
dynamic
>
result
=
json
.
decode
(
response
.
toString
());
BaseHttpModel
httpModel
=
BaseHttpModel
.
fromJson
(
result
);
if
(
httpModel
.
isSuccess
())
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment