gpt4 book ai didi

c# - 音频的Base64验证

转载 作者:行者123 更新时间:2023-12-02 23:29:29 25 4
gpt4 key购买 nike

实际上,我想在API中接收base64,但我想允许仅包含音频的base64 ,下面是我验证base64的功能,但是在Convert.FromBase64String(base64String);之前,我想验证该base64是音频。

public static int CheckAudio(string base64String)
{

if (string.IsNullOrEmpty(base64String) || base64String.Length % 4 != 0
|| base64String.Contains(" ") || base64String.Contains("\t") || base64String.Contains("\r") || base64String.Contains("\n"))
return (int)ConfezzStatusCode.InValidParamter;

try
{
Convert.FromBase64String(base64String);
return (int)HttpStatusCode.OK;
}
catch
{
// Handle the exception
return (int)ConfezzStatusCode.InValidParamter;
}
}

注意:我只允许音频文件。

最佳答案

您可以使用file signatures来检测它是否是已知的音频格式。像这样:

// Decode the whole string to make sure it is a valid Base64
byte[] data = Convert.FromBase64String(base64String);

// Just for readability store it as HEX
// You might want to store only the file header (eg, 64 bytes)
string hex = BitConverter.ToString(data);

if (hex.StartsWith("49-44-33"))
{
Console.WriteLine("mp3");
}
else if (hex.StartsWith("66-4C-61-43"))
{
Console.WriteLine("flac");
}
else {
Console.WriteLine("unknown:" + hex);
}

有关更多想法,请检查:
  • Using .NET, how can you find the mime type of a file based on the file signature not the extension
  • In C#, how can I know the file type from a byte[]?
  • 关于c# - 音频的Base64验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56394141/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com