Introduction
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
- public async Task<IActionResult> DownloadFile(string serverFileUrl)
- {
- var webUrl = "https://sharepoint.com/sites/509847/";
- var fileUrl = serverFileUrl;
- var accessToken = "bdau123213hu3huBYAG321huh3h87df87fsns8BB87Y8suhds9098dshdjdnsm9088sdjkdhas897dsah";
- using (var client = new HttpClient())
- {
- client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
- var requestUrl = String.Format("{0}/_api/web/getfilebyserverrelativeurl('{1}')/$value", webUrl, fileUrl);
- var response =await client.GetAsync(requestUrl, HttpCompletionOption.ResponseHeadersRead);
- var fileContent = response.Content.ReadAsByteArrayAsync().Result;
- return File(fileContent, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);
- }
- }
Here, I have used HttpClient for sending HTTP Request and receiving HTTP Response from a SharePoint REST API.
Output
Below is the screenshot of the downloaded file.
Conclusion
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.
Thanks.