简体   繁体   中英

HttpPostedFileBase to byte[] how to keep the encoding?

So, the scenario is this: A user uploads a file, my code turns this file into an array of bytes, and than the array is passed to an external API. And this works fine.

The problem is that this file contains special characters like æ,ø,å, and when the byte[] is converted into characters again, these characters are replaced by "?".

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
}

Can I add some encoding-info to the byte-array or the InputStream, or is it the API's responsibillity to make sure that the text is properly encoded when converting the byte[] back to characters?

It is API's responsibility to properly convert byte array to chars. If you have access to API code than you should add encoding parameters to UploadDocument method

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM