import 'package:dio/dio.dart';
import 'package:wechat_assets_picker/wechat_assets_picker.dart';
import 'package:image_picker/image_picker.dart';
InkWell(
onTap: () => _getImageFromGallery(),
child: Text("54555555555555555"),
)
Future _getImageFromGallery() async {
var image = await ImagePicker().pickImage(source: ImageSource.gallery);
File file = File(image!.path);//需要把XFile转换成File
Uploadimage(file);
}
Uploadimage(File file) async {
// 单个文件上传,格式一定要注意
var formData = FormData.fromMap({
'file': await MultipartFile.fromFile(file.path, filename: 'upload.txt'),
"userId": 22,
});
// 多个文件上传
// FormData.fromMap({
// 'files': [
// MultipartFile.fromFileSync('./example/upload.txt', filename: 'upload.txt'),
// MultipartFile.fromFileSync('./example/upload.txt', filename: 'upload.txt'),
// ]
// });
try {
Response response;
Dio dio = new Dio();
response = await dio.post(
url,
data: formData,
options: Options(contentType: "multipart/form-data"),
);
print(response);
} catch (e) {
return print(e);
}
}
|