//Downdload file
[HttpPost("download")]
public async Task<IActionResult> DownloadFile([FromBody] FileUpload Upload)
{
Config.DefaultResponse response = new Config.DefaultResponse();
try
{
string siteUrl = "";
var RelativeFilePath = "/sites/YourSiteName/YourLibraryName" + Upload.FolderName + "/" + Upload.FileName;
var apiUrl = $"{siteUrl}/_api/web/GetFileByServerRelativePath(decodedurl='{RelativeFilePath}')/$value";
var accessTokenResult = await GetAccessToken();
if (accessTokenResult is ObjectResult accessTokenObjectResult && accessTokenObjectResult.Value is string accessToken)
{
using (var httpClient = new HttpClient())
{
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var downloadResponse = await httpClient.GetAsync(apiUrl);
var responseText = downloadResponse.Content.ReadAsStringAsync();
if (downloadResponse.IsSuccessStatusCode)
{
var filecontent = await downloadResponse.Content.ReadAsByteArrayAsync();
return Ok(filecontent);
}
else
{
response = new Config.DefaultResponse(500, "Failed to download", responseText);
return BadRequest(response);
}
}
}
else
{
response = new Config.DefaultResponse(500, "Failed", "Something Wrong occured");
return BadRequest(response);
}
}
catch
{
response = new Config.DefaultResponse(500, "Failed", "Something Wrong occured");
return BadRequest(response);
}
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter