Stefan Srbljanin
Stefan Srbljanin

Reputation: 161

Base64 encoding in C# - Issue while sending an attachemnt via REST POST method

Is it possible in C#/Xamarin to encode byte array to Base64 like it's possible in Java?!
byte[] encBytes = Base64.encodeBase64(buffer);
So I'm not looking to turn bytes in Base64 string I'm looking for byte array encoded with Base64, as is done in Java

Edit to reflect answer:

byte[] buffer = new byte[(int)fileLen];
int offset = 0;int numRead = 0;
while ( offset < buffer.length && ( numRead = input.read(buffer, offset, buffer.length - offset)) >= 0) 
{
 offset += numRead;
}
byte[] encBytes = Base64.encodeBase64(buffer);


So, a buffer is populated with data from a file and then encoded to base64.
I don't see a way to read a file from a source, like in that snipet.

Edit2:
The issue, it seems, is not just the encoding but sending the encoded file to the server.
When sending a file, it creates a file on the server but file is either corrupted (pdf and excel) or blank (docx).

Upvotes: 1

Views: 513

Answers (1)

Eren Durmuş
Eren Durmuş

Reputation: 17

like this in java

public static String toBase64(String value){
byte[] message = value.getBytes("UTF-8");
}

in C#

 byte[] array = UTF8Encoding.UTF8.GetBytes(value);
 string base64 =Convert.FromBase64String(array);

Upvotes: 1

Related Questions