Commit 05678ce9 authored by “Icebear”'s avatar “Icebear”

添加多张图片上传解决办法

parent d1cf6148
......@@ -39,6 +39,7 @@ mixin IService {
_analysisApiException(iEntity);
}else{
iEntity = new IEntity(code: HttpCode.NORMAL_ERROR,msg:e.toString());
}
}
return iEntity;
......
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", imagePath: avatarPath,onResult: (data,errorMsg){
static uploadAvatarRequest(List images, OnResult onResult) {
return HttpManager.getInstance().request('file/upload', parameters: null,method: "POST", images: images,onResult: (data,errorMsg){
if(onResult != null){
onResult(data, errorMsg);
}
......
......@@ -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,27 +87,30 @@ class _LoginState extends State<LoginRequestPage> {
}
});
}
void _requestListData() {
if(UserModel.getModel() == null){
Fluttertoast.showToast(msg:'请登录');
return;
}
void _requestListData() {
if (UserModel.getModel() == null) {
Fluttertoast.showToast(msg: '请登录');
return;
}
Map<String, String> map = new Map<String, String>();
map['page'] = '0';
LoginApi.requestListData(map, (httpModel, error) {
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: 100,
width: 300,
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: 100,
width: 150,
height: 50,
child: ElevatedButton(
child: Text("请求列表数据"),
......
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){
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, //图片路径
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);
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()) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment