Arthi RK
Arthi RK

Reputation: 1

How to read larger PPT files (from 100MB to 2 GB ) from Sharepoint Site

I need to read a large PowerPoint file (ranging from over 100 MB to 2 GB) from a SharePoint site and load it into a stream for use elsewhere.

I am attempting to read a large file from a SharePoint site and have tried two different approaches in my local code C# code.

1st Approach

using Microsoft.Sharepoint.Client dll:

Web web = clientContext.Web;
clientContext.Load(web, website => website.ServerRelativeUrl);
clientContext.ExecuteQuery();
var serverRelativeURL = docURL.Replace("https://microsoft.sharepoint.com", "").Replace(" ", "%20");

var file = web.GetFileByServerRelativeUrl(serverRelativeURL);
clientContext.Load(file, f => f.Properties);
clientContext.ExecuteQuery();

var stream1 = file.OpenBinaryStream();
await clientContext.ExecuteQueryAsync();

But file.OpenBinaryStream() can able to read smaller size file only such as KB files.

2nd Approach

 using PnP.Core.Model.SharePoint dll: 

 using (PnPContext pnpCoreContext = PnPCoreSdk.Instance.GetPnPContext(clientContext))
 {
     var absoluteUrl = new Uri(new Uri("https://microsoft.sharepoint.com"), serverRelativeURL);
     string relativepath = absoluteUrl.ToString().Replace("https://microsoft.sharepoint.com", "");
     IFile fileToDownload = await pnpCoreContext.Web.GetFileByServerRelativeUrlAsync(relativepath);

     Stream downloadedContentStream = await fileToDownload.GetContentAsync(true);

     // Initialize a MemoryStream to hold the downloaded content
     using (MemoryStream memoryStream = new MemoryStream())
     {
             var bufferSize = 2 * 1024 * 1024;  // 2 MB buffer
             var buffer = new byte[bufferSize];
             int read;

             // Download the file bytes in chunks and write them to the MemoryStream
             while ((read = await downloadedContentStream.ReadAsync(buffer, 0, buffer.Length)) != 0)
             {
                 await memoryStream.WriteAsync(buffer, 0, read);
             }

             // Reset the position of the MemoryStream to the beginning
             memoryStream.Position = 0;
     }

But in this case its able to read 100MB file but not more than that.

Expectation: The goal is to read a large PowerPoint file from a SharePoint site and store it into a stream.

Upvotes: 0

Views: 61

Answers (0)

Related Questions