'Sharepoint download a file in base64 using .Net Core

I need to download a file from Sharepoint server. I was previously using Asp.net MVC and there were methods like File.OpenBinaryDirect(). But after adding portal library to my .net core application I am unable to find any method like File.OpenBinaryDirect().

Microsoft.SharePoint.Client.ClientContext clientContext = 
    new Microsoft.SharePoint.Client.ClientContext(server);
Microsoft.SharePoint.Client.FileInformation f = 
    Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext, serverrelative);

clientContext.ExecuteQuery();

using (var fileStream = new FileStream(@"d:\prettyimage.jpg", FileMode.Create))
f.Stream.CopyTo(fileStream);

What should I use now to download a file using .net core 2.2.



Solution 1:[1]

We are using this package for .NET Core. Unfortunately it is almost a year old I see. It is really strange that there are no official .NET Core compatible SharePoint nuget packages.

And this code to get a Stream of a file on SharePoint:

public async Task<(ListItem, Stream)> GetFile(string customerNumber, string listTitle, string listItemId)
{
    ClientContext clientContext = await GetClientContextCustomerAsync(customerNumber);

    // Load invoice
    List invoicesList = clientContext.Web.Lists.GetByTitle(listTitle);
    ListItem invoicelistItem = invoicesList.GetItemById(listItemId);
    clientContext.Load(invoicelistItem, i => i.File);
    await ExecuteRequest(clientContext);

    // Load invoice file contents
    ClientResult<Stream> binaryStream = invoicelistItem.File.OpenBinaryStream();
    clientContext.Load(invoicelistItem.File);
    await ExecuteRequest(clientContext);

    return (invoicelistItem, binaryStream.Value);
}

public SharePointOnlineCredentials GetSharePointOnlineCredentials(string username, string password)
{
    return new SharePointOnlineCredentials(username, password);
}

public ClientContext GetClientContext(string siteUrl, string username, string password)
{
    SharePointOnlineCredentials sharePointOnlineCredentials = GetSharePointOnlineCredentials(username, password);

    return new ClientContext(siteUrl)
    {
        Credentials = sharePointOnlineCredentials
    };
}

private async Task<ClientContext> GetClientContextCustomerAsync(string customerDebtorNumber)
{
    var customerSiteUrl = await GetCustomerSiteUrl(customerDebtorNumber);

    if (customerSiteUrl == null)
    {
        _logger.LogWarning($"CustomerSiteUrl is null for customerDebtorNumber: {customerDebtorNumber}");
        return null;
    }

    return GetClientContext(customerSiteUrl, _appSettingsSettings.SharePoint.Username, _appSettingsSettings.SharePoint.Password);
}

Solution 2:[2]

You could use the file.OpenBinaryStream() to read/write files stored in SharePoint, below is my demo code for your reference:

 Microsoft.SharePoint.Client.File file = context.Web.GetFileByUrl("https://tenant.sharepoint.com/sites/michael/Shared%20Documents/aa.txt");
 context.Load(file);
 context.ExecuteQuery();       

 Microsoft.SharePoint.Client.ClientResult<Stream> mstream = file.OpenBinaryStream();
 context.ExecuteQuery();
    
 using (System.IO.StreamReader sr = new System.IO.StreamReader(mstream.Value))
 {
   String line = sr.ReadToEnd();
   Console.WriteLine(line);
 }

reference: MS docs Q&A

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Sanjeevi Subramani