uploadImageBytes method
Implementation
Future<UploadJsonResult> uploadImageBytes(
String? fileName, List<int>? fileBytes, String? mimeType) async {
// 检测是否含有后缀,如果没有后缀,则添加 .png后缀
if (!fileName!.contains(".")) {
fileName += '.png';
}
// 格式化日期时间
String formattedDateTime = BytedeskUtils.formatedTimestampNow();
// 拼接文件名
String fileNameNew = '${formattedDateTime}_$fileName';
Uri uploadUri = BytedeskUtils.getHostUri('/visitor/api/v1/upload/file');
debugPrint("uploadUri ${uploadUri.toString()}");
// BytedeskUtils.printLog(
// "uploadImage fileName $fileName, username $username, upload Url $uploadUrl");
// web browser 浏览器中不支持此种上传图片方式
// Unsupported operation: MultipartFile is only supported where dart:io is available.
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
..files.add(http.MultipartFile.fromBytes('file', fileBytes!,
filename: fileName, contentType: MediaType.parse(mimeType!)));
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("web upload image responseJson $responseJson");
//
return UploadJsonResult.fromJson(responseJson);
}