Flutter Dio将MultipartFile对象动态地添加到Map [英] Flutter Dio add MultipartFile object to Map dynamicaly

查看:767
本文介绍了Flutter Dio将MultipartFile对象动态地添加到Map的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有自定义数据:

File avatar = File('path/to/file');

Map<String, dynamic> data = {
  'name': 'John',
  'avatar': avatar
}

如何将我的数据作为FormData对象发送到服务器?

How I can send this my data as FormData object to server?

我尝试通过循环数据来创建 MultipartFile 类的对象,但在我的情况下,将文件路径作为文件的字符串实例发送.这是我的代码:

I tried create object of MultipartFile class by looping my data but in my case sending file path as string instance of file. Here is my code:

data.forEach((key, value) {
  if (value is File) {
    String fileName = value.path.split('/').last;
    data.update(key, (value) async {
      return await MultipartFile.fromFile(value.path, filename: fileName);
    });
  }
});

FormData formData = FormData.fromMap(data);
var response = await dio.post("/send", data: formData);

但是使用 Dio ,我可以上传类似这样的文件:

But using Dio I can upload file something like this:

FormData formData = FormData.fromMap({
    "name": "wendux",
    "age": 25,
    "file": await MultipartFile.fromFile(file.path,filename: fileName)
});

为什么我不能动态地将 MultipartFile 添加到我的 Map 中?

Why I can't dynamically add MultipartFile to my Map ?

推荐答案

您可以不带dio发送它.在这里,我编写了包括库和响应在内的整个代码.由于我的情况下dio无法正常工作,并且您的情况与我的情况非常相似,请测试以下代码.

You can send it without dio. Here I write the whole code including the libraries and response.Please test the below code becuase in my case dio is not working and your case is very similar to my case.

只需通过简单的http请求发送

import 'package:http/http.dart' as http;
import 'package:http_parser/http_parser.dart';
import 'package:mime/mime.dart';

   Map<String, String> headers = {
      'Content-Type': 'multipart/form-data',        
    };

    Map jsonMap = {
      "name": "wendux",
      "age": 25,          
    };
 String imagePath, // add the image path in varible 

 var request = http.MultipartRequest('POST', Uri.parse(url));
    request.headers.addAll(headers);
    request.files.add(
      http.MultipartFile.fromBytes(
        'orderStatusUpdate',
        utf8.encode(json.encode(jsonMap)),
        contentType: MediaType(
          'application',
          'json',
          {'charset': 'utf-8'},
        ),
      ),
    );

    if (imagePath != null) {
      final mimeTypeData = lookupMimeType(imagePath).split('/');
      final file = await http.MultipartFile.fromPath('images', imagePath,
          contentType: MediaType(mimeTypeData[0], mimeTypeData[1]));
      print((mimeTypeData[0].toString())); // tells picture or not
      print((mimeTypeData[1].toString())); // return extention
      request.files.add(file);
    }

    http.StreamedResponse streamedResponse = await request.send();
    var response = await http.Response.fromStream(streamedResponse);
    print(response.statusCode);
    print(response.body);

这篇关于Flutter Dio将MultipartFile对象动态地添加到Map的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆