0

我正在尝试将来自 Flutter 上的 Multi Image Picker 插件的多个文件发送到我的服务器,为此我正在尝试使用 Dio 发送它。但是 Multipart 标签也没有上传文件。如何进行?

    Future<Response> _uploadFile() async {

    var catUpload = jsonEncode(incluirCategoriasParaUpload());
    final FormData formData = FormData.fromMap({
      "action": 'add_Anuncio',
      "filial_id": "1",
      "titulo": tituloControler.text,
      "descricao": descricaoControler.text,
      "categorias": catUpload
    });

    for (var val in listaImagensParaUpload) {
      ByteData byteData = await val.getByteData();
      Uint8List pngBytes = byteData.buffer.asUint8List();
      formData.files.add(
          MapEntry("arquivos",
              await MultipartFile.fromBytes(pngBytes, filename: "teste")
          )
      );
    }

    Dio dio = new Dio();
    dio.interceptors.add(alice.getDioInterceptor());
    return await dio
        .post(URL_WS + WS_PAGE,
        data: formData,
        options: Options(method: 'POST', responseType: ResponseType.json))
        .timeout(Duration(seconds: 2000));
//        .catchError(dioError);


  }
4

2 回答 2

5

您必须将此 for 循环声明为 toList()。

这是一个如何制作的例子。

formData = FormData.fromMap({
  'id': '1',
  'title': 'myTitle',
  'files': [
            for (var file in listFiles)
            {await MultipartFile.fromFile(item.path, filename: 'fileName')}
             .toList()
           ]
});

我正在使用与 MultiPartFile 一起工作的 Dio 库。

于 2020-07-15T03:25:56.647 回答
0

我使用 dio 和 image_picker 依赖项发布此解决方案。它肯定会奏效。我花了 2 天时间来解决这个问题。

FormData formData = new FormData.fromMap({
"name": "Max",
"location": "Paris",
"age": 21,
"image[]": [
await MultipartFile.fromFile(
  _imageFile.path,
),
await MultipartFile.fromFile(
  _imageFile.path,
),
], 
});

print(FormData1().then((value) {
print(value);
 }));
response = await dio.post(

"http://143.110.244.110/radius/frontuser/eventsubmitbutton",
data: formData,
onSendProgress: (received, total) {
if (total != -1) {
  print((received / total * 100).toStringAsFixed(0) + '%');
}
},
);
 print(response);
于 2021-07-22T08:19:46.260 回答