upload - Flutter:如何使用 formdata 和 multipartfile 上传多个资产?
问题描述
我仍在学习颤振,使用multi_image_picker
,formdata
和. 将资产上传到服务器时出现错误dio
。如果没有图像,我可以成功上传,但null
如果包含任何图像,我会收到错误消息。
请帮我。
此代码用于选择图像。
Future<void> loadAssets() async {
String error = 'No Error Detected';
try{
resultList = await MultiImagePicker.pickImages(
maxImages: 4,
enableCamera: true,
selectedAssets: images,
cupertinoOptions: CupertinoOptions(takePhotoIcon: "chat"),
);
} on Exception catch (e){
error = e.toString();
}
if(!mounted) return;
setState((){
images = resultList;
error = error;
});
}
此代码用于将 formData 上传到服务器。
upload(List<Asset> imageList) async {
var formData = FormData.fromMap({
"userId": _id,
"post": _postContent,
"tags": _tags.join(",").toString(),
});
if(null != imageList) {
for (int i = 0; i < imageList.length; i++) {
ByteData byteData = await imageList[i].getByteData();
List<int> imageData = byteData.buffer.asUint8List();
formData.files.addAll([
MapEntry(
"images[]",
MultipartFile.fromBytes(
imageData,
filename: 'load_image',
contentType: MediaType("image", "png"),
)),
]);
}
}
// this is where I pass the data to the server using dio
AuthService().userForumPost(formData).then((val) async {
print(val.data);
print('Form Submitted Successfully');
_confirmationDialog(context);
});
}
解决方案
推荐阅读
- python - Django ModelForm 未显示在模板中
- python - 在python中获得不同的输出
- c# - 如何在 Xamarin Forms UWP 中获得更好的图像缩放?
- docker - Docker 容器安装在 Windows 上
- c++ - 在 Ubuntu 上的 VS 代码上运行 C++ 代码并收到此错误 collect2: error: ld returned 1 exit status
- r - 提取数字和以下文本并在 R 中创建多个新列
- bash - 在 bash 中使用 awk/grep 迭代 YAML 文件中的特定键和特定变量
- c++ - 标准输入中的 C++ Windows 密钥检测
- powerbi - Power BI Dax - 如果为 false,则返回原始单元格值
- python - __str__ 返回非字符串(NoneType 类型),因为该字段为空