ToBase64String,C# 3.5
While converting a zip file to base64 encoded string,Which is needed to sent mail with MIME support I use
var encodedContent = Convert.ToBase64String(Encoding.ASCII.GetBytes(File.ReadAllText(zipFilePath)));
However all the attachements I receive is corrupted.It开发者_如何学Python seems base64 encoding is not correct. Can anyone advise on this.
File.ReadAllText
is designed to read text, and therefore applies encodings (and that overload applies heuristics to determine the encoding). This is why you're then needing to "un-encode" to get bytes.
Applying different encodings in that way is likely to lead to changes in the byte stream (that's the point of encodings).
Better to read bytes directly out of the file, with something like File.ReadAllBytes
which can be passed directly to ToBase64String
.
精彩评论