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

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

parent d1cf6148
...@@ -39,6 +39,7 @@ mixin IService { ...@@ -39,6 +39,7 @@ mixin IService {
_analysisApiException(iEntity); _analysisApiException(iEntity);
}else{ }else{
iEntity = new IEntity(code: HttpCode.NORMAL_ERROR,msg:e.toString()); iEntity = new IEntity(code: HttpCode.NORMAL_ERROR,msg:e.toString());
} }
} }
return iEntity; return iEntity;
......
import 'Network/HttpManager.dart'; import 'Network/HttpManager.dart';
import 'Network/BaseHttpModel.dart';
import 'UserModel.dart'; import 'UserModel.dart';
/// 所有接口类 /// 所有接口类
...@@ -17,8 +16,8 @@ class LoginApi { ...@@ -17,8 +16,8 @@ class LoginApi {
} }
///上传头像 ///上传头像
static uploadAvatarRequest(String avatarPath, OnResult onResult) { static uploadAvatarRequest(List images, OnResult onResult) {
return HttpManager.getInstance().request('file/upload', parameters: null,method: "POST", imagePath: avatarPath,onResult: (data,errorMsg){ return HttpManager.getInstance().request('file/upload', parameters: null,method: "POST", images: images,onResult: (data,errorMsg){
if(onResult != null){ if(onResult != null){
onResult(data, errorMsg); onResult(data, errorMsg);
} }
......
...@@ -20,7 +20,8 @@ class _LoginState extends State<LoginRequestPage> { ...@@ -20,7 +20,8 @@ class _LoginState extends State<LoginRequestPage> {
// This widget is the root of your application. // This widget is the root of your application.
final phoneTextField = TextEditingController(); final phoneTextField = TextEditingController();
final pwdTextField = TextEditingController(); final pwdTextField = TextEditingController();
PickedFile _image; PickedFile _image1;
PickedFile _image2;
@override @override
void dispose() { void dispose() {
...@@ -56,19 +57,29 @@ class _LoginState extends State<LoginRequestPage> { ...@@ -56,19 +57,29 @@ class _LoginState extends State<LoginRequestPage> {
Fluttertoast.showToast(msg:'退出成功'); Fluttertoast.showToast(msg:'退出成功');
} }
Future getImage() async { void _getImage(int type) async {
if(UserModel.getModel() == null){ if(UserModel.getModel() == null){
Fluttertoast.showToast(msg:'请登录'); Fluttertoast.showToast(msg:'请登录');
return; return;
} }
var image = await ImagePicker.platform.pickImage(source: ImageSource.gallery); var image = await ImagePicker.platform.pickImage(source: ImageSource.gallery);
_upLoadImage(image);//上传图片
setState(() { setState(() {
_image = image; if(type == 0){
_image1 = image;
}else {
_image2 = image;
}
}); });
} }
_upLoadImage(PickedFile image) async { _upLoadImage() {
LoginApi.uploadAvatarRequest(image.path, (httpModel, error){ 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){ if(error != null){
Fluttertoast.showToast(msg: error); Fluttertoast.showToast(msg: error);
}else{ }else{
...@@ -76,27 +87,30 @@ class _LoginState extends State<LoginRequestPage> { ...@@ -76,27 +87,30 @@ class _LoginState extends State<LoginRequestPage> {
} }
}); });
} }
void _requestListData() {
if(UserModel.getModel() == null){ void _requestListData() {
Fluttertoast.showToast(msg:'请登录'); if (UserModel.getModel() == null) {
return; Fluttertoast.showToast(msg: '请登录');
} return;
}
Map<String, String> map = new Map<String, String>(); Map<String, String> map = new Map<String, String>();
map['page'] = '0'; map['page'] = '0';
LoginApi.requestListData(map, (httpModel, error) { LoginApi.requestListData(map, (httpModel, error) {
if (error != null) { if (error != null) {
Fluttertoast.showToast(msg: error); Fluttertoast.showToast(msg: error);
} else { } else {
List examList = httpModel.data["content"].map((m) => new ExamListModel.fromJson(m)).toList(); List examList = httpModel.data["content"]
if(examList.length > 0){ .map((m) => new ExamListModel.fromJson(m))
.toList();
if (examList.length > 0) {
ExamListModel model = examList[0]; ExamListModel model = examList[0];
Fluttertoast.showToast(msg: model.toJson().toString(),timeInSecForIos:5); Fluttertoast.showToast(
msg: model.toJson().toString(), timeInSecForIos: 5);
} }
} }
}); });
} }
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( return Scaffold(
...@@ -159,22 +173,38 @@ class _LoginState extends State<LoginRequestPage> { ...@@ -159,22 +173,38 @@ class _LoginState extends State<LoginRequestPage> {
), ),
Container( Container(
margin: EdgeInsets.only(top: 50), margin: EdgeInsets.only(top: 50),
width: 100, width: 300,
height: 100, height: 100,
child: ElevatedButton( child: Row(
child: Text("上传头像"), mainAxisAlignment: MainAxisAlignment.start,
onPressed: getImage, 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( Container(
margin: EdgeInsets.only(top: 0), margin: EdgeInsets.only(top: 0),
width: 100, width: 150,
height: 100, height: 50,
child: Image.asset(_image != null ? _image.path : Images.lake), child: ElevatedButton(
child: Text("上传多张图"),
onPressed: _upLoadImage,
),
), ),
Container( Container(
margin: EdgeInsets.only(top: 10), margin: EdgeInsets.only(top: 10),
width: 100, width: 150,
height: 50, height: 50,
child: ElevatedButton( child: ElevatedButton(
child: Text("请求列表数据"), child: Text("请求列表数据"),
......
import 'dart:convert'; import 'dart:convert';
import 'dart:ffi'; import 'dart:ffi';
import 'dart:io';
import 'package:dio/dio.dart'; import 'package:dio/dio.dart';
import 'package:netrain_flutter_app/zhangfeng/Network/BaseHttpModel.dart'; import 'package:netrain_flutter_app/zhangfeng/Network/BaseHttpModel.dart';
import 'package:netrain_flutter_app/zhangfeng/UserModel.dart'; import 'package:netrain_flutter_app/zhangfeng/UserModel.dart';
...@@ -78,30 +79,38 @@ class HttpManager { ...@@ -78,30 +79,38 @@ class HttpManager {
request(String url, request(String url,
{parameters, {parameters,
method = 'GET', method = 'GET',
OnResult onResult,String imagePath}) async { OnResult onResult,List images}) async {
parameters = parameters ?? {}; parameters = parameters ?? {};
if(onResult==null){ if(onResult==null){
return; return;
} }
try { try {
FormData formdata; FormData formdata;
if(imagePath != null){ if(images != null){
var name = imagePath.substring(imagePath.lastIndexOf("/") + 1, imagePath.length); List<MultipartFile> imageList = new List<MultipartFile>();
var suffix = name.substring(name.lastIndexOf(".") + 1, name.length); for (String imagePath in images) {
formdata = FormData.fromMap({ var name = imagePath.substring(imagePath.lastIndexOf("/") + 1, imagePath.length);
"file": await MultipartFile.fromFile( var suffix = name.substring(name.lastIndexOf(".") + 1, name.length);
imagePath, //图片路径 MultipartFile multipartFile = await MultipartFile.fromFile(
imagePath,
//这个字段要有,否则后端接收为null
filename: name, filename: name,
) );
imageList.add(multipartFile);
}
formdata = new FormData.fromMap({
//后端要用multipartFiles接收参数,否则为null
"multipartFiles" : imageList
}); });
} }
parameters = new Map<String, dynamic>.from(parameters); 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) { 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, 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()); Map<String, dynamic> result = json.decode(response.toString());
BaseHttpModel httpModel = BaseHttpModel.fromJson(result); BaseHttpModel httpModel = BaseHttpModel.fromJson(result);
if (httpModel.isSuccess()) { 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