Flutter Dio网络框架4.0.6升级到最新版本时,文件上传报错

错误提示:
org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object ‘commentDto’ on field baseFile’: rejected value []; codes [typeMismatch.commentDto.baseFile,typeMismatch.baseFile,typeMismatch.[Lorg.springframework.web.multipart.MultipartFile;,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [commentDto.baseFile,baseFile]; arguments []; default message [baseFile]]; default message [Failed to convert property value of type ‘java.lang.String’ to required type ‘org.springframework.web.multipart.MultipartFile[]’ for property ‘baseFile’; nested exception is java.lang.IllegalStateException: Cannot convert value of type ‘java.lang.String’ to required type ‘org.springframework.web.multipart.MultipartFile’ for property ‘baseFile[0]’: no matching editors or conversion strategy found]

解决方式:

MultipartFile.fromFileSync(
            data["image"],
            filename: suffix,
            contentType: MediaType('application', 'octet-stream'),//这行是新增的
          )
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Flutter中使用Dio库进行文件可以通过以下步骤实现: 首先,确保已经在项目的`pubspec.yaml`文件中添加了Dio库的依赖: ```yaml dependencies: dio: ^4.0.0 ``` 然后,在需要上文件的地方引入Dio库: ```dart import 'package:dio/dio.dart'; ``` 接下来,创建一个Dio的实例并配置它: ```dart var dio = Dio(); ``` 然后,使用Dio的`FormData`类来创建一个表单数据对象,并添加需要上文件: ```dart FormData formData = FormData.fromMap({ 'file': await MultipartFile.fromFile(file.path, filename: 'upload.jpg'), }); ``` 这里的`file`是一个`File`对象,表示需要上文件。 最后,使用Dio的`post`方法发送请求,并将表单数据作为参数递给它: ```dart Response response = await dio.post( 'https://example.com/upload', data: formData, ); ``` 这里的`https://example.com/upload`是上文件的接口地址,你需要替换为实际的地址。 完整的示例代码如下: ```dart import 'dart:io'; import 'package:dio/dio.dart'; void main() async { var dio = Dio(); File file = File('path/to/file.jpg'); FormData formData = FormData.fromMap({ 'file': await MultipartFile.fromFile(file.path, filename: 'upload.jpg'), }); Response response = await dio.post( 'https://example.com/upload', data: formData, ); print(response.data); } ``` 通过以上步骤,你就可以使用Dio库在Flutter中上文件了。记得根据实际情况修改代码中的文件路径和上接口地址。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值