uploadVideoBytes method

Future<UploadJsonResult> uploadVideoBytes(
  1. String? fileName,
  2. List<int>? fileBytes,
  3. String? mimeType
)

Implementation

Future<UploadJsonResult> uploadVideoBytes(
    String? fileName, List<int>? fileBytes, String? mimeType) async {
  // FIXME: image_picker有bug,选择视频后缀为.jpg,此处替换一下
  // String? fileName = filePath!.split("/").last.replaceAll(".jpg", ".mp4");
  // 格式化日期时间
  String formattedDateTime = BytedeskUtils.formatedTimestampNow();
  // 拼接文件名
  String fileNameNew = '${formattedDateTime}_$fileName';

  Uri uploadUri = BytedeskUtils.getHostUri('/visitor/api/v1/upload/file');
  debugPrint("uploadUri ${uploadUri.toString()}");

  //
  Map<String, String> headers = {
    HttpHeaders.contentTypeHeader: "video/mp4",
  };
  var request = http.MultipartRequest('POST', uploadUri)
    ..fields['file_name'] = fileNameNew
    ..fields["file_type"] = BytedeskUtils.getFileType(fileNameNew)
    ..fields["is_avatar"] = "false"
    ..fields["kb_type"] = BytedeskConstants.UPLOAD_TYPE_CHAT
    ..fields["category_uid"] = ""
    ..fields["kb_uid"] = ""
    ..fields["client"] = client
    ..headers.addAll(headers)
    ..files.add(http.MultipartFile.fromBytes('file', fileBytes!,
        filename: fileName,
        // FIXME: 设置不起作用?
        contentType: MediaType('video', 'mp4')));

  http.Response response =
      await http.Response.fromStream(await request.send());
  // debugPrint("Result: ${response.body}");

  //解决json解析中的乱码问题
  Utf8Decoder utf8decoder = const Utf8Decoder(); // fix 中文乱码
  //将string类型数据 转换为json类型的数据
  final responseJson = json.decode(utf8decoder.convert(response.bodyBytes));
  debugPrint("upload Video responseJson $responseJson");
  //
  return UploadJsonResult.fromJson(responseJson);
}