简体   繁体   English

Flutter 在特定正文中上传带有 MultipartRequest 的图像

[英]Flutter Upload image with MultipartRequest inside specific body

I need to upload a list of attachments inside an array with this structure:我需要在具有以下结构的数组中上传附件列表:

[
'description' => ['sometimes', 'string'],
'attachments' => ['sometimes', 'array'],
'attachments.*.file' => ['required', 'file'],
]

I'm using package http, and to upload files only allow me to put inside 'files', i'm using this code and it works to upload a file inside files, but I need that structure.我正在使用 package http,并且上传文件只允许我放入“文件”,我正在使用此代码,它可以在文件中上传文件,但我需要这种结构。

 if (json != null) {
      request.fields.addAll(json);
    }

    for (var item in filesPath) {
      request.files.add(await http.MultipartFile.fromPath(
        'file',
        item,
      ));
    } 

There is any way to achieve this with this plugin?有什么方法可以用这个插件实现这一点?

Please try this请试试这个

void uploadImage() async {
  // Show loader
  // open a byteStream
  var stream = new
  http.ByteStream(DelegatingStream.typed(file.openRead()));
  // get file length
  var length = await file.length();
  Map<String, String> headers = {
    "Accept": "application/json",
    "Authorization": token
  }; // ignore this headers if there is no authentication

  // string to uri
  var uri = Uri.parse(Constants.BASE_URL);

  // create multipart request
  var request = new http.MultipartRequest("POST", uri);

  // if you need more parameters to parse, add those like this 
  // to the API request
  request.fields["orderId"] = orderID.toString();

  // multipart that takes file.. here this "file" is a key of the 
 // API request
  var multipartFile = new http.MultipartFile('file', stream,
      length,
      filename: basename(file.path));

  //add headers
  request.headers.addAll(headers);

  // add file to multipart
  request.files.add(multipartFile);

  // send request to upload image
  await request.send().then((response) async {
    // listen for response
    response.stream.transform(utf8.decoder).listen((value) {
      print(value);
      setState(() {
        if (response.statusCode == 200) {
          print('uploaded');
        } else {
          print('failed');
        }
      });

      // Hide loader

    });
  }).catchError((e) {
    print(e);
    // Hide loader
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试在 flutter 中使用 MultipartRequest 上传文件时出现握手异常 - Handshake Exception when trying to upload a file using MultipartRequest in flutter 如何在Flutter中取消通过http.MultipartRequest()发送的正在进行的文件上传? - How to cancel ongoing file upload sent with http.MultipartRequest() in Flutter? 当从 flutter 中的 MultipartRequest 发送图像时无法检测到图像 - Failed to detect the image when an image sent form MultipartRequest in flutter Flutter:使用二进制正文上传图像 - Flutter: upload image using binary body 使用 MultiPartRequest 上传多张图片:内容大小低于指定的 contentLength - Multiple image upload using MultiPartRequest: Content size below specified contentLength 单元测试(Flutter 中的 MultipartRequest) - Unit Test (MultipartRequest in Flutter) 如何将 memory 中的图像作为二进制图像上传到 Flutter web 中的主体上? - How to upload an image in memory, as a binary image on the body in Flutter web? Flutter DIO:使用带有 Dio package 的二进制正文上传图像 - Flutter DIO: upload image using binary body with Dio package 如何在颤振中在 MultipartRequest 上插入标头 - how to insert headers on MultipartRequest in flutter 如何在 flutter 中的 MultipartRequest 中返回数据 - How to return the data in MultipartRequest in flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM