2

I'm trying to upload text and pdf files code below but I'm receiving the error in the content size. I'm following
the code in the document but I'm not sure if I'm doing it the right way. Thanks in advance

Future<void> _postCallCreate(String fileNames, String urlSegment) async {
Map<String, String> requestHeaders = {
  'Content-type': 'application/json',
  'Accept': 'application/json',
  'Authorization': _token,
};

final url = Constants.BASE_URL + urlSegment;
try {
  var request = http.MultipartRequest('POST', Uri.parse(url));
  request.files.add(http.MultipartFile(
      'dto',
      File(fileNames).readAsBytes().asStream(),
      File(fileNames).lengthSync(),
      filename: fileNames.split("/").last));

  print(request.files.first);

  request.headers.addAll(requestHeaders);
  var res = await request.send();

  print(res.reasonPhrase);
  notifyListeners();
} catch (error) {
  print(error);
  throw error;
}
}
Waqar Younus
  • 97
  • 2
  • 9
  • There's a convenience function that handles creating the `MultipartFile` from a path: https://pub.dev/documentation/http/latest/http/MultipartFile/fromPath.html Give that a try, as it reduces errors. – Richard Heap May 11 '20 at 20:51
  • @RichardHeap I have also try this mentioned link method but its showing same error – Waqar Younus May 11 '20 at 21:13

0 Answers0