参考:https://www.cnblogs.com/yangyxd/p/9168271.html
import 'dart:io' ;
import 'dart:math' show Random;
import 'dart:async';
import 'package:http/http.dart' as Http;
/**
* 请求响应数据
*/
class MsgResponse {
int code; // 状态代码,0 表示没有错误
Object data; // 数据内容,一般为字符串
String errmsg; // 错误消息
MsgResponse([this.code = 0, this.data = null, this.errmsg = ""]);
}
Map _makeHttpHeaders([String contentType,
String accept,
String token,
String XRequestWith,
String XMethodOverride]) {
Map headers = new Map();
int i = 0;
if (contentType.isNotEmpty) {
i++;
headers["Content-Type"] = contentType;
}
if (accept.isNotEmpty) {
i++;
headers["Accept"] = accept;
}
/* if (token.isNotEmpty) {
i++;
headers["Authorization"] = "bearer " + token;
}
*/
if (XRequestWith.toString().isNotEmpty) {
i++;
headers["X-Requested-With"] = XRequestWith;
}
if (XMethodOverride.toString().isNotEmpty) {
i++;
headers["X-HTTP-Method-Override"] = XMethodOverride;
}
if (i == 0) return null;
// print(headers.toString());
return headers;
}
/** HTTP POST 上传文件 */
Future httpUploadFile(final String url, final File file, {
String accept = "*/*", String token, String field = "picture-upload", String file_contentType, // 默认为null,自动获取
}) async {
try {
List bytes = await file.readAsBytes();
return await httpUploadFileData(url, bytes,
accept: accept,
token: token,
field: field,
file_contentType: file_contentType,
filename: file.path);
} catch (e) {
return new MsgResponse(699, null, e.toString());
}
}
/** HTTP POST 上传文件 */
Future httpUploadFileData(final String url, final List filedata, {
String accept = "*/*", String token, String field = "picture-upload", String file_contentType, // 默认为null,自动获取
String filename,
}) async {
//try {
List bytes = filedata;
var boundary = _boundaryString();
String contentType = 'multipart/form-data; boundary=$boundary';
Map headers =
_makeHttpHeaders(contentType, accept, token); //, "XMLHttpRequest");
// 构造文件字段数据
print("$boundary");
String data =
'--$boundary\r\nContent-Disposition: form-data; name="$field"; ' +
'filename="${getFileFullName(filename)}"\r\nContent-Type: ' +
'${(file_contentType == null) ? getMediaType(getFileExt(filename).toLowerCase()): file_contentType}\r\n\r\n';
print("$data");
var controller = new StreamController>(sync: true);
controller.add(data.codeUnits);
controller.add(bytes);
controller.add("\r\n--$boundary--\r\n".codeUnits);
controller.close();
bytes = await new Http.ByteStream(controller.stream).toBytes();
//print("bytes: \r\n" + UTF8.decode(bytes, allowMalformed: true));
Http.Response response =
await Http.post(url, headers: headers, body: bytes);
print(response.body);
return MsgResponse(0, response.body);
/*if (response.statusCode == 200) {
return new MsgResponse(0, response.body);
} else
return new MsgResponse(response.statusCode, response.body);*/
/*} catch (e) {
return new MsgResponse(699, null, e.toString());
}*/
}
/** 生成随机字符串 */
String randomStr([int len = 8, List chars = _BOUNDARY_CHARACTERS]) {
var list = new List.generate(len, (index) => chars[_random.nextInt(chars.length)], growable: false);
return new String.fromCharCodes(list);
}
const List _BOUNDARY_CHARACTERS = const [
0x30,
0x31,
0x32,
0x33,
0x34,
0x35,
0x36,
0x37,
0x38,
0x39,
0x61,
0x62,
0x63,
0x64,
0x65,
0x66,
0x67,
0x68,
0x69,
0x6A,
0x6B,
0x6C,
0x6D,
0x6E,
0x6F,
0x70,
0x71,
0x72,
0x73,
0x74,
0x75,
0x76,
0x77,
0x78,
0x79,
0x7A,
0x41,
0x42,
0x43,
0x44,
0x45,
0x46,
0x47,
0x48,
0x49,
0x4A,
0x4B,
0x4C,
0x4D,
0x4E,
0x4F,
0x50,
0x51,
0x52,
0x53,
0x54,
0x55,
0x56,
0x57,
0x58,
0x59,
0x5A
];
const int _BOUNDARY_LENGTH = 48;
final Random _random = new Random();
String _boundaryString() {
var prefix = "---DartFormBoundary";
var list = new List.generate(_BOUNDARY_LENGTH - prefix.length, (index) => _BOUNDARY_CHARACTERS[_random.nextInt(_BOUNDARY_CHARACTERS.length)], growable: false);
return "$prefix${new String.fromCharCodes(list)}";
}
const mimeLists = {
'css': 'text/css',
'html': 'text/html',
'js':'text/javascripte',
'xml': 'text/xml',
'gif': 'image/gif',
'jpg': 'image/jpeg',
'jpeg': 'image/jpeg',
'png': 'image/jpeg',
'json': 'application/json'
};
getMediaType(final String fileExt) {
String Content = mimeLists["$fileExt"];
if(Content.isEmpty){
Content= 'text/plain';
}
return Content;
}
/*getMediaType(final String fileExt) {
switch (fileExt) {
case ".jpg":
case ".jpeg":
case ".jpe":
return new MediaType("image", "jpeg");
case ".png":
return new MediaType("image", "png");
case ".bmp":
return new MediaType("image", "bmp");
case ".gif":
return new MediaType("image", "gif");
case ".json":
return new MediaType("application", "json");
case ".svg":
case ".svgz":
return new MediaType("image", "svg+xml");
case ".mp3":
return new MediaType("audio", "mpeg");
case ".mp4":
return new MediaType("video", "mp4");
case ".htm":
case ".html":
return new MediaType("text", "html");
case ".css":
return new MediaType("text", "css");
case ".csv":
return new MediaType("text", "csv");
case ".txt":
case ".text":
case ".conf":
case ".def":
case ".log":
case ".in":
return new MediaType("text", "plain");
}
return null;
}*/
/**
* 获取文件名
*/
String getFileFullName(filename){
var index1 = filename.toString().lastIndexOf("/");
if(index1.toString().isEmpty){
index1=0;
}
var name = filename.substring(index1+1);
//print(name);
return name;
}
/**
* 获取扩展名
*/
String getFileExt(filename){
var index = filename.toString().lastIndexOf(".");
var suffix = filename.toString().substring(index+1);
//print(suffix);
return suffix;
}
调用
import 'package:image_picker/image_picker.dart';//图片处理
//图库:ImageSource.gallery 相机:ImageSource.camera
Future getImage() async {
var image = await ImagePicker.pickImage(source: ImageSource.gallery);
if(image.toString().isEmpty){
return null;
}
_uploadFile_vod(image);
/*setState(() {
imgList.add(image);
});*/
}
_uploadFile_vod(image) async {
//存取值需要
//String url = "http://172.19.35.49:8080/wcms/front/announcement/uploadPhoto";
String url = "http://172.19.35.37:8081/sgylbzj/front/jbinfo/uploadfile";
print(image);
//print(image.path);
Future response = httpUploadFile(url, image);
//print(response.toString());
}
import 'dart:io';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';//toast
import 'package:image_picker/image_picker.dart';//从相册里面选择图片或者拍照获取照片
class HeadImageUploadPage extends StatefulWidget {
@override
_HeadImageUploadPageState createState() => _HeadImageUploadPageState();
}
class _HeadImageUploadPageState extends State {
File _image;
Future getImage() async {
var image = await ImagePicker.pickImage(source: ImageSource.gallery);
_upLoadImage(image);//上传图片
setState(() {
_image = image;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Image Picker Example'),
),
body: Center(
child: _image == null
? Text('No image selected.')
: Image.file(_image),
),
floatingActionButton: FloatingActionButton(
onPressed: getImage,
tooltip: 'Pick Image',
child: Icon(Icons.add_a_photo),
),
);
}
//上传图片
_upLoadImage(File image) async {
String path = image.path;
var name = path.substring(path.lastIndexOf("/") + 1, path.length);
FormData formData = new FormData.from({
"file": new UploadFileInfo(new File(path), name)
});
Dio dio = new Dio();
var respone = await dio.post("路径", data: formData);
if (respone.statusCode == 200) {
Fluttertoast.showToast(
msg: "图片上传成功",
gravity: ToastGravity.CENTER,
textColor: Colors.grey);
}
}
}