gpt4 book ai didi

flutter - 如何使用带有 mime 类型的 dio 在 flutter 中使用多部分表单数据上传图像

转载 作者:行者123 更新时间:2023-12-03 02:52:50 24 4
gpt4 key购买 nike

这是我的代码

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 类型设置为图像时,我无法获取任何文件。当我设置为任何时,我能够获取图像

最佳答案

最简单的方法是使用 flutter.dev 的 mime_type 插件. 添加这个导入

import 'package:dio/dio.dart';
import 'package:http_parser/http_parser.dart';
import 'package:mime_type/mime_type.dart';

添加特定的内容类型。

  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()));

关于flutter - 如何使用带有 mime 类型的 dio 在 flutter 中使用多部分表单数据上传图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59360281/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com