Adnan
Adnan

Reputation: 1293

ClientException (Content size exceeds specified contentLength. 10911 bytes written while expected 5965

i am getting ClientException (Content size exceeds specified contentLength. 10911 bytes written while expected 5965. error on trying to upload m4a file to my server. i tried to send the exact same request from postman and it works just fine. i also was able to send imgs and videos using the exact same request in flutter. i tracked the send method and it gives the error in the method 'send' in the Client class in the framework this is my request code:

static Future<String> uploadFile(
      {Attachment attachment,
      List<Attachment> attachments,
      String toUserIdForMessage}) async {
    Uri _uri = Uri.parse(globalUrl + file + 'Uploads');
    http.MultipartRequest _reqss = http.MultipartRequest(
      'POST',
      _uri,
    );
    Attachment _attForHeaders = attachment ?? attachments[0];
    _reqss.headers.addAll(await headersWToken);
    _reqss.fields['ownerType'] =
        _attForHeaders.attachmentOwner.index.toString();
    _reqss.fields['ownerId'] = _attForHeaders.ownerId.toString();
    _reqss.fields['toUserIdForMessage'] = toUserIdForMessage;
    if (attachment != null && attachment.path != null)
      _reqss.files.add(
        await http.MultipartFile.fromPath(
          attachment.path,
          attachment.path,
        ),
      );
    if (attachments != null && attachments.length != 0) {
      for (Attachment att in attachments) {
        _reqss.files.add(
          await http.MultipartFile.fromPath(
            att.path,
            att.path,
          ),
        );
      }
    }
    var _response = await (_reqss.send());
    var _re = _response.stream.bytesToString();
    return _re;
  }

Upvotes: 8

Views: 4573

Answers (3)

Finding Nemo
Finding Nemo

Reputation: 1

static Future<http.Response> httpuploadProfileFormData({
required String url,
String? path,
required SaveUserInfo saveInfo,
Map<String, String>? headers}) async {
var request = http.MultipartRequest('POST', Uri.parse(url));
// Add headers if provided
if (headers != null) {
  request.headers.addAll(headers);
}

// Add form fields
saveInfo.toJson().forEach((key, value) {
  request.fields[key] = value;
});

// Add file if provided
if (path != null) {
  final file = File(path);
  final fileSize = await file.length();
  final fileStream = http.ByteStream(file.openRead());
  final multipartFile = http.MultipartFile(
    'imageUpload', // key for the file
    fileStream,
    fileSize,
    filename: path.split('/').last,
  );

  request.files.add(multipartFile);
}

// Send the request
final streamedResponse = await request.send();

// Convert the streamed response to a regular response
final response = await http.Response.fromStream(streamedResponse);

// Handle response
if (response.statusCode == 200) {
  return response; // Successful response
} else {
  throw Exception('Failed to upload data: ${response.statusCode}');
}  }

use this to solve the issue, if you are using DIO package. There is no solution provided in DIO package side. so you can use http instead of DIO

Upvotes: 0

Alexey Lo
Alexey Lo

Reputation: 451

I had the same problem.

I used MultipartFile.fromBytes instead of MultipartFile.fromPath and it worked.

Upvotes: 0

Adnan
Adnan

Reputation: 1293

this error has occured when i was trying to record a voice message and upload it. the problem was about not closing the recorder. so when the request is being prepared the recorder continued to record and thus appeared the size difference problem.

Upvotes: 7

Related Questions