none
Stream to Base64String - Out of memory exception RRS feed

  • Question

  • Help appreciated in resolving this issue,  below source throws System.OutOfMemory exception 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());
            }

    Thanks

    Sunday, January 7, 2018 2:49 PM

All replies

  • Check if this works:

       return Convert.ToBase64String( stream.GetBuffer(), stream.Length);

    How large is the file?

    • Edited by Viorel_MVP Sunday, January 7, 2018 6:01 PM
    Sunday, January 7, 2018 5:59 PM
  • Thanks for your help Viorel, the file is 515MB.

    Tuesday, January 9, 2018 10:50 AM
  • The odds of you being able to allocate a contiguous block of 515MB of memory to generate the array needed by that stream.ToArray call is slim unless you're compiling for x64 and then it is just plain inefficient. But then you're going to base64 encode that which means you're using up 1 GB of memory just for the array and string (another 515 MB continguous block of characters). This is just not going to work well. Please explain why you need to base64 encode a very large file rather than using a stream which is the correct approach to handling large streams of data.

    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, January 9, 2018 2:52 PM
    Moderator
  • What are you going to do with that?  Base64 isn't much use except in email, and you certainly cannot email a 700MB attachment.

    Tim Roberts, Driver MVP Providenza & Boekelheide, Inc.

    Tuesday, January 9, 2018 6:18 PM
  • Thanks Tim.

    Not used in email processing, the source is used in Azure Web Job for processing SharePoint Online document.

    Help appreciated.

    Monday, January 15, 2018 7:04 PM
  • Check if this works:

       return Convert.ToBase64String( stream.GetBuffer(), stream.Length);

    How large is the file?

    Thanks, but the above throws syntax error, tried the below & still the same 'Out of memory' exception for 515MB file. 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);
            }

    Thursday, January 25, 2018 8:40 PM