【问题标题】:i am trying to upload multiple images using dio mathod and multipart but its giving error我正在尝试使用 dio mathod 和 multipart 上传多个图像,但它给出了错误
【发布时间】:2022-07-21 22:31:30
【问题描述】:

当我打印 _images 列表时,它显示“多部分实例”任何人都可以帮助我解决这个问题,当我发送 imageFileList 列表时,服务器不接受此列表,并且 请提及多部分在发送图像时的用途 这是来自服务器错误 500 的服务器响应的响应错误

import 'dart:io';
import 'package:dio/dio.dart';
import 'package:shared_preferences/shared_preferences.dart';

var token;

Dio dio = Dio();
List<dynamic>? _images = [];
var apiURL = 'https://denga.r3therapeutic.com/public/api/addpost';
FormData formData = FormData();
Future<String> adminAddproperty({
  title,
  address,
  price,
  area,
  bedrooms,
  bathrooms,
  parking,
  other,
  description,
  List<File>? imageFileList,
  context,
}) async {
  for (int i = 0; i < imageFileList!.length; i++) {
    var image = imageFileList[i].path;

    _images!.add(
        await MultipartFile.fromFile(image, filename: image.split('/').last));
  }

  FormData formData = FormData.fromMap({
    'title': title,
    'address': address,
    'price': price,
    'area': area,
    'bedrooms': bedrooms,
    'bathrooms': bathrooms,
    'parking': parking,
    'others': other,
    'description': description,
    'images[]': imageFileList,
  });

  SharedPreferences pref = await SharedPreferences.getInstance();
  token = pref.getString('token');
  print('AdminApisToken =$token');
  print('_images');

  Response responce;

  responce = await dio.post(apiURL,
      data: formData,
      options: Options(headers: {
        HttpHeaders.authorizationHeader: "Bearer $token",
      }));
  print('Responce: $responce');
  return '';
}
    

【问题讨论】:

    标签: flutter api dart multipart dio


    【解决方案1】:

    我遇到了类似的情况,我通过将内容类型添加到MultipartFile 来解决它,如下所示:

    await MultipartFile.fromFile(picture.path, contentType: MediaType.parse(image.mimeType)
    

    或者硬编码类型MediaType('image', 'jpeg')MediaTypepackage:http_parser/http_parser.dart 包的一部分。

    希望对你有帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-11-03
      • 1970-01-01
      • 1970-01-01
      • 2019-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多