小编Jij*_*son的帖子

如何使用 dio 和 mime 类型在 flutter 中使用多部分表单数据上传图像

这是我的代码

void _uploadFile(filePath1, filepath2) async {
    // Get base file name
    String fileName1 = basename(filePath1.path);
    var ij = lookupMimeType(fileName1);
    print('this is $ij');
    print("File base name: $fileName1");
    print(filePath1);
    String fileName2 = basename(filepath2.path);

    try {
      FormData formData = new FormData.fromMap({
        "text": 'hello',
        "productImages": [
          await MultipartFile.fromFile(filePath1.path, filename: fileName1),
          await MultipartFile.fromFile(filepath2.path, filename: fileName2),
        ]
      });

      Response response = await Dio().post("http://192.168.18.25:8080/test",
          data: formData,);
      print("File upload response: $response");

      // Show the incoming message in snakbar
      _showSnakBarMsg(response.data['message']);
    } catch (e) {
      print("Exception Caught: $e");
    }
  } …
Run Code Online (Sandbox Code Playgroud)

multipartform-data image-upload dart flutter dio

4
推荐指数
1
解决办法
2万
查看次数

标签 统计

dart ×1

dio ×1

flutter ×1

image-upload ×1

multipartform-data ×1