简体   繁体   English

流到Base64String-内存不足异常

[英]Stream to Base64String - Out of memory exception

Help appreciated in resolving the 'Out of memory exception' from the below source at line return Convert.ToBase64String(stream.ToArray()) ; 在从以下源代码中解决以下行中的“内存不足异常”的帮助: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());
    }

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. 假设stream.ToArray()已成功执行且Convert.ToBase64String崩溃,则您始终可以将批处理转换为64字符串,并将其存储在可以重新加载的位置。 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. 如果您的应用程序在stream.ToArray()上崩溃,那么您将需要允许应用程序使用更多的内存(如果有),或者在从流中加载时应用块加载的想法。

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);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM