Base64 String

by Ivan Gerasimenko

JavaScript

byte[] file = File.ReadAllBytes("C:\\Users\\gerasimenko-is\\Documents\\1940.pdf");
string file64 = Convert.ToBase64String(file);

string[] arr64String = StringSplitter.SplitByLength(file64, 300000).ToArray();

// Write the stream contents to a new file named "AllTxtFiles.txt".
for (int i = 0; i < arr64String.Length; i++) {
    using (StreamWriter outfile = new StreamWriter(String.Format("C:\\Users\\gerasimenko-is\\Documents\\OutBase64_{0}.txt", i)))
    {
        outfile.Write(arr64String[i]);
    }
}
            

String line = "";
try
{
    using (StreamReader sr = new StreamReader("C:\\Users\\gerasimenko-is\\Documents\\InBase64.txt"))
    {
        line = sr.ReadToEnd();
    }
}
catch (Exception ex)
{
    Console.WriteLine("The file could not be read:");
    Console.WriteLine(ex.Message);
}

byte[] byteArr = Convert.FromBase64String(line);

using (var imageFile = new FileStream("C:\\Users\\gerasimenko-is\\Documents\\Out.pdf", FileMode.Create))
{
    imageFile.Write(byteArr, 0, byteArr.Length);
    imageFile.Flush();
}





====================


public static class StringSplitter
{
    public static IEnumerable<string> SplitByLength(this string str, int maxLength)
    {
        for (int index = 0; index < str.Length; index += maxLength)
        {
            yield return str.Substring(index, Math.Min(maxLength, str.Length - index));
        }
    }
}