首页 > 解决方案 > ClientException(内容大小超过指定的 contentLength。10911 字节写入而预期 5965

问题描述

我收到ClientException(内容大小超过指定的contentLength。10911字节写入而预期5965。尝试将m4a文件上传到我的服务器时出错。我试图从邮递员发送完全相同的请求,它工作得很好。我也能够在颤振中使用完全相同的请求发送 imgs 和视频。我跟踪了 send 方法,它在框架中的 Client 类中的方法“send”中给出了错误,这是我的请求代码:

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;
  }

标签: flutterhttpdart

解决方案


当我尝试录制语音消息并上传时发生此错误。问题是关于不关闭录音机。所以当请求正在准备时,记录器继续记录,因此出现了大小差异问题。


推荐阅读