实际上,我想在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;
}
}
注意:我只允许音频文件。
答案 0 :(得分:0)
var audioBase64 = "data:audio/mpeg;base64,/+MYxAAEaAIEeUAQAgBgNgP/////KQQ/////Lvrg+lcWYHgtjadzsbTq+yREu495tq9c6v/7vt/of7mna9v6/btUnU17Jun9/+MYxCkT26KW+YGBAj9v6vUh+zab//v/96C3/pu6H+pv//r/ycIIP4pcWWTRBBBAMXgNdbRaABQAAABRWKwgjQVX0ECmrb///+MYxBQSM0sWWYI4A++Z/////////////0rOZ3MP//7H44QEgxgdvRVMXHZseL//540B4JAvMPEgaA4/0nHjxLhRgAoAYAgA/+MYxAYIAAJfGYEQAMAJAIAQMAwX936/q/tWtv/2f/+v//6v/+7qTEFNRTMuOTkuNVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV";
var X1 = audioBase64.split(":");
var X2 = X1[1].split(";");
var X3 = X2[0].split("/");
if(X3[0] == "audio"){
alert("this is an audio file");
}
else{
alert("Not an audio file");
}
答案 1 :(得分:0)
您可以使用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);
}
有关更多想法,请检查: