Нажатие на Multipart API для отправки изображений и видео на сервер, иногда API работает успешно, а иногда выдает исключение FormatException: Unexpected end of input. Когда я получаю исключение, запись была создана в базе данных, но она не получила никакого изображения.
Future<Map<String, dynamic>> callMultipartPostAPI(
List<CustomMultipartObject> files) async {
String url = this.baseurl + this.name;
Uri uri = Uri.parse(url);
var request = http.MultipartRequest("POST", uri);
apiHeader["Content-Type"] = "multipart/form-data";
apiHeader["Accept"] = "application/json";
apiHeader["Connection"] = "keep-alive";
apiHeader["Accept-Encoding"] = "gzip, deflate, br";
request.headers.addAll(apiHeader);
request.fields.addAll(apiBody);
files.forEach((file) {
var stream =
new http.ByteStream(DelegatingStream(file.file.openRead()));
file.file.length().then((length) {
var multipartFile = new http.MultipartFile(file.param, stream, length,
filename: basename(file.file.path));
request.files.add(multipartFile);
});
});
try {
var streamedResponse = await request.send();
var response = await http.Response.fromStream(streamedResponse);
print("Code: " + response.statusCode.toString());
Map<String, dynamic> fResponse = json.decode(response.body);
return fResponse;
} on FormatException catch (error) {
print("FormatException: " + error.message);
throw error;
} on SocketException catch (error) {
print("SocketException: " + error.message);
throw error;
} on ArgumentError catch (error) {
print("ArgumentError: " + error.message);
throw error;
} catch (error) {
print("Exception: " + error);
throw error;
}
}