这是我的代码
void _uploadFile(filePath1, filepath2) async {
// Get base file name
String fileName1 = basename(filePath1.path);
var ij = lookupMimeType(fileName1);
print('this is $ij');
print("File base name: $fileName1");
print(filePath1);
String fileName2 = basename(filepath2.path);
try {
FormData formData = new FormData.fromMap({
"text": 'hello',
"productImages": [
await MultipartFile.fromFile(filePath1.path, filename: fileName1),
await MultipartFile.fromFile(filepath2.path, filename: fileName2),
]
});
Response response = await Dio().post("http://192.168.18.25:8080/test",
data: formData,);
print("File upload response: $response");
// Show the incoming message in snakbar
_showSnakBarMsg(response.data['message']);
} catch (e) {
print("Exception Caught: $e");
}
}
当mime类型设置为image时,我无法获取任何文件。 设置为任何我都可以获取图像
答案 0 :(得分:4)
最简单的方法是使用flutter.dev中的mime_type插件。 添加此导入
import 'package:dio/dio.dart';
import 'package:http_parser/http_parser.dart';
import 'package:mime_type/mime_type.dart';
添加特定的contentType。
String mimeType = mime(fileName);
String mimee = mimeType.split('/')[0];
String type = mimeType.split('/')[1];
Dio dio = new Dio();
dio.options.headers["Content-Type"] = "multipart/form-data";
FormData formData = new FormData.fromMap({
'file':await MultipartFile.fromFile(filePath,
filename: fileName, contentType: MediaType(mimee, type))
});
Response response = await dio
.post('http://192.168.18.25:8080/test', data: formData)
.catchError((e) => print(e.response.toString()));