c# – HttpPostedFileBase到byte []如何保持编码?

所以,情况是这样的:

用户上传文件,我的代码将此文件转换为字节数组,然后将数组传递给外部API.这很好用.

问题是该文件包含æ,ø,å等特殊字符,当byte []再次转换为字符时,这些字符将替换为“?”.

public void UploadFile(HttpPostedFileBase file){
    var binary = new byte[file.ContentLength];
    file.InputStream.Read(binary, 0, file.ContentLength;
    var result = API.UploadDocument(binary); //Passes the file to the external API
}

我可以在字节数组或InputStream中添加一些encoding-info,还是API的责任是确保在将byte []转换回字符时正确编码文本?

最佳答案 API负责将字节数组正确转换为字符.如果您有权访问API代码,则应将编码参数添加到UploadDocument方法

点赞