簡體   English   中英

流到Base64String-內存不足異常

[英]Stream to Base64String - Out of memory exception

在從以下源代碼中解決以下行中的“內存不足異常”的幫助: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());
    }

假設stream.ToArray()已成功執行且Convert.ToBase64String崩潰,則您始終可以將批處理轉換為64字符串,並將其存儲在可以重新加載的位置。 然后將轉換后的數據塊一一加載,並將其存儲到結果文件中。 如果您的應用程序在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);
        }
         byte[] docBytes = stream.ToArray();            
         return Convert.ToBase64String(docBytes);
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM