0

我正在尝试从我的颤振移动应用程序上传多个文件。这是我的邮递员截图。这在 postMan 上运行良好。但我在颤振移动应用程序上收到 400 个错误。我找不到问题出在哪里? 在此处输入图像描述

在此处输入图像描述

现在这是我的颤振移动应用上传的代码。

 if(images.length>0){
      for (int i = 0; i < images.length; i++) {
        var path = await FlutterAbsolutePath.getAbsolutePath(images[i].identifier);
        String fileName = path.split('/').last;
        var file = await MultipartFile.fromFile(path, filename:fileName);
        multipart.add(file);
      }

      FormData imageFormData = FormData.fromMap(
          {
            "feedId": value.id,
            "images": multipart,
            "userInformationsId": value.userInformationId
          });
      print(multipart.length);
      uploadFeedPicture(imageFormData);
  }

 Future<String> uploadFeedPicture(FormData _imageformData) async{

    String at = await _apiProvider.getAccessToken();
    Dio dio = new Dio();
   // dio.options.headers['accept'] = 'application/json';
  //  dio.options.headers["content-Type"] = "multipart/form-data";
    dio.options.headers["Authorization"] = "Bearer ${at}";
    dio.options.baseUrl = getBaseUrl();

    var _baseUrl = getBaseUrl();
     await dio.post('/api/feed/upload', data: _imageformData,  options: Options(
         followRedirects: false,
         validateStatus: (status) { return status < 500; }
     ), onSendProgress: (int sent, int total) {
      print("$sent $total");
    },).then((value) {
      print(value.data);

      print(value.headers);

    }
    ).catchError((error) => print(error) );




 }

我得到了这个回应。

{statusCode: 400, error: Bad Request, message: Invalid request payload input}

请帮我看看问题出在哪里?我尝试更改内容类型但不工作。

4

1 回答 1

0

在尝试了很多不同的方法后,我找到了解决方案。这里是。

if(value.id != null){

    String at = await _apiProvider.getAccessToken();
    Map<String, String> headers = { "Authorization": "Bearer ${at}"};
    var uri = Uri.parse('$baseUrl/api/feed/upload');
    var  request = http.MultipartRequest('POST', uri);
    request.headers.addAll(headers);
    if(images.length>0){
      for (int i = 0; i < images.length; i++) {
        var path = await FlutterAbsolutePath.getAbsolutePath(images[i].identifier);
       // String fileName = path.split('/').last;

        var file = await MultipartFile.fromPath("images",path);

          request.files.add(file);
      }
      request..fields['feedId'] = value.id;
      request..fields['userInformationsId'] = value.userInformationId;


      var response = await request.send();
      if (response.statusCode == 200) {
        Navigator.pop(context);
        showSuccessToast("Posted succesfull");
        counter.update(0);
        Navigator.push(
          context,
          MaterialPageRoute(builder: (context) => HomePage()),
        );
      }else{
        showErrorToast("Upload image failed");
      }


    }
  }
于 2020-08-06T05:49:27.767 回答