0

Help appreciated in resolving the 'Out of memory exception' from the below source at line return Convert.ToBase64String(stream.ToArray());

private static string GetSPFileBinary(ClientContext ctx, string fileUrlPath)
    {
        ctx.RequestTimeout = Int32.MaxValue;
        var spFile = ctx.Web.GetFileByServerRelativeUrl(fileUrlPath);
        var spFileContent = spFile.OpenBinaryStream();
        ctx.Load(spFile);
        ctx.ExecuteQuery();
        MemoryStream stream = new MemoryStream();
        if (spFileContent != null && spFileContent.Value != null)
        {
            spFileContent.Value.CopyTo(stream);
        }
        return Convert.ToBase64String(stream.ToArray());
    }
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
sukumarraju
  • 91
  • 1
  • 9

2 Answers2

0

Assuming that stream.ToArray() was executed successfully and Convert.ToBase64String crashes, you could always convert a batch to 64 string and store it somewhere from where you can reload it. And then load the converted chunks one-by-one and store them into the result file. If your application crashes on stream.ToArray(), then you will need to allow your application to use more memory if available or apply the chunk loading idea when you load from stream as well.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
0

even the below throws the same old 'Out of memory exception', help appreciated.

private static string GetSPFileBinary(ClientContext ctx, string fileUrlPath)
    {
        ctx.RequestTimeout = Int32.MaxValue;
        var spFile = ctx.Web.GetFileByServerRelativeUrl(fileUrlPath);
        var spFileContent = spFile.OpenBinaryStream();
        ctx.Load(spFile);
        ctx.ExecuteQuery();
        MemoryStream stream = new MemoryStream();
        if (spFileContent != null && spFileContent.Value != null)
        {
            spFileContent.Value.CopyTo(stream);
        }
         byte[] docBytes = stream.ToArray();            
         return Convert.ToBase64String(docBytes);
    }
sukumarraju
  • 91
  • 1
  • 9