[英]'utf-8' codec can't decode byte 0x80 in position 28: invalid start byte
I have a wav file that I converted to binary and then changed it to base64 using C# and then post that base64 string to a python flask API so I can decode it and change back the base64 string to a wav file so I can save in a folder, I am getting this error below:我有一个 wav 文件,我将其转换为二进制文件,然后使用 C# 将其更改为 base64,然后将该 base64 字符串发布到 python flask API,这样我就可以将其解码并在 a8 文件中将 wav 字符串改回 8648348文件夹,我在下面收到此错误:
'utf-8' codec can't decode byte 0x80 in position 28: invalid start byte 'utf-8' 编解码器无法解码 position 中的字节 0x80 28:起始字节无效
Please see my code below:请在下面查看我的代码:
public static void copy_to_watch_folder(string filepath, string filename)
{
try
{
AppDomain.CurrentDomain.SetPrincipalPolicy(System.Security.Principal.PrincipalPolicy.WindowsPrincipal);
var destinationFolder = SSKAccess.mapped_dir_path;
byte[] audio_file = File.ReadAllBytes(filepath);
var s = Convert.ToBase64String(audio_file);
string jsonData = JsonConvert.SerializeObject(new { Result = "OK", File = s, FileName = filename, token= SSKAccess.token }, Formatting.None);
StringContent sC2 = new StringContent(jsonData, System.Text.Encoding.UTF8, "application/json");
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.BaseAddress = new Uri(ConnectionSetup.connectionStr);
try
{
var response = client.PostAsync("putWavFile", sC2).Result;
if (response.IsSuccessStatusCode)
{
Logs.WriteToFile($"{ DateTime.Now } : {response.Content.ReadAsStringAsync()}");
}
}
catch (Exception exc)
{
Logs.WriteToFile($"HttpError : {exc.Message}");
}
//System.IO.File.Copy(filepath, $"{destinationFolder}{filename}", true);
}
catch (IOException copyError)
{
Logs.WriteToFile($"{DateTime.Now} - CopyError: {copyError.Message}");
}
}
My Python code looks like:我的 Python 代码如下所示:
@app.route('/api/Call/putWavFile', methods=['POST'])
def PutWavFile():
if not request.json or not 'token' in request.json:
abort(400)
try:
filename = request.json['FileName']
binaryData = request.json['File']
file_decoded = base64.decodebytes(binaryData.encode('utf-8').strip())
_file = file_decoded.decode()
path_to_save = os.path.join(os.getcwd(), 'Watch', filename)
with open(path_to_save, mode='wb') as f:
f.write(file_decoded)
return jsonify({"FileCreated": "Success"})
except Exception as exc:
print(f"FileError: {str(exc)}")
return jsonify( {"Error": f"{str(exc)}"} )
Please assist and thanks in advance.请协助并提前致谢。
Thanks y'all for your assistance but finally I got the solution.谢谢大家的帮助,但最终我得到了解决方案。 It turns out all I needed was the following line to decode the base64 string coming from my C# API call:事实证明,我只需要以下行来解码来自我的 C# API 调用的 base64 字符串:
file_decoded = base64.b64decode(binaryData)
Thanks again for those who tried to assist.再次感谢那些试图提供帮助的人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.