Download File From SharePoint REST API In .Net Core


Generally, SharePoint is a web-based collaborative platform which is used as a document management and storage system. We can also use it as a secure place to store, organize, share and access information from any device. There are so many SharePoint REST APIs for CRUD operations and to manipulate data in SharePoint. In this blog, I am describing about how to download files from Sharepoint using REST APIs. Just as in normal applications or websites, we can also download files from SharePoint using SharePoint REST APIs.
Source Code 
  1. public async Task<IActionResult> DownloadFile(string serverFileUrl)  
  2. {  
  3.     var webUrl = "";  
  4.     var fileUrl = serverFileUrl;  
  5.     var accessToken = "bdau123213hu3huBYAG321huh3h87df87fsns8BB87Y8suhds9098dshdjdnsm9088sdjkdhas897dsah";  
  6.     using (var client = new HttpClient())  
  7.     {  
  8.         client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);  
  9.         var requestUrl = String.Format("{0}/_api/web/getfilebyserverrelativeurl('{1}')/$value", webUrl, fileUrl);  
  10.         var response =await client.GetAsync(requestUrl, HttpCompletionOption.ResponseHeadersRead);  
  11.         var fileContent = response.Content.ReadAsByteArrayAsync().Result;  
  12.         return File(fileContent, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);  
  13.     }  
  14. }  
Here, I have used HttpClient for sending HTTP Request and receiving HTTP Response from a SharePoint REST API.
Below is the screenshot of the downloaded file. 


Finally, in this blog we can learn about downloading file from SharePoint using REST Api. There are a lot of Share Point REST APIs for Accessing Folders, Files, and Documents. I hope this will be helpful for you guys.