[英]Upload image flutter web with dio formdata
I am trying using file_picker and dio packages to upload files as form data.我正在尝试使用 file_picker 和 dio 包将文件作为表单数据上传。
This is for flutter web and it seems MultipartFile.fromFile
is not accepted.这是针对 flutter web 的,似乎MultipartFile.fromFile
未被接受。
What I tried is the following:我尝试的是以下内容:
if (result != null) {
for (var file in result.files) {
final formData = FormData.fromMap({
...someOtherData,
'file': File(file.name), // <------ I guess this is where the issue is, I also tried file instead of File(file.name)
});
dio.post(
url,
data: formData,
);
}
}
If anyone is still wondering how to get it working on both mobile and web (This is using the image_picker's PickedFile as the image variable type):如果有人仍然想知道如何让它在移动设备和 web 上工作(这是使用 image_picker 的 PickedFile 作为图像变量类型):
FormData body;
final bytes = await image.readAsBytes();
final MultipartFile file = MultipartFile.fromBytes(bytes, filename: "picture");
MapEntry<String, MultipartFile> imageEntry = MapEntry("image", file);
body.files.add(imageEntry);
** The catch is that the filename is required on web and is automatically assigned on mobile.** ** 问题是文件名在 web 上是必需的,在移动设备上是自动分配的。**
Ok, I found it, leaving here for someone having the same problem好的,我找到了,留给遇到同样问题的人
if (result != null) {
for (var file in result.files) {
final formData = FormData.fromMap({
...someOtherData,
'file': MultipartFile.fromBytes(file.bytes as List<int>)
});
dio.post(
url,
data: formData,
);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.