Hello I am trying to download the excelfiles which was uploaded through a batch to the Azure Storage Account --> File Storage.
Can you provide me a code sample to download the excel files from the Azure File Storage and send the response to the front end.
Unfortunately the below code does not work
public async Task<ActionResult> DownloadFileFromStorage(string url)
{
FileStreamResult result = null;
try
{
var splittedUrl = url.Split('/');
var fileClient = await _fileStorageService.GetShareClientAsync(splittedUrl[splittedUrl.Length - 2].ToString(), splittedUrl.Last());
ShareFileDownloadInfo download = await fileClient.DownloadAsync();
//var fileContent = await _fileStorageService.GetFileReferenceAsync(splittedUrl[splittedUrl.Length - 2].ToString(), splittedUrl.Last());
byte[] fileContents = null;
using (MemoryStream stream = new MemoryStream())
{
await download.Content.CopyToAsync(stream);
fileContents = stream.ToArray();
var fileName = url.Split('/').Last();
return File(fileContents, "application/csv", "soxfile_2023-06-01.csv");
}
}
catch (Exception ex)
{
throw;
}
}
FileStorageService.cs function
public async Task<ShareFileClient> GetShareClientAsync(string shareName, string fileName)
{
var configuration = await _configurationService.GetConfigurationAsync();
var storageCredentials = new StorageCredentials(configuration.Azure.StorageAccountName,
configuration.Azure.StorageAccountKey);
var storageAccount = new CloudStorageAccount(storageCredentials, true);
ShareClient share = new ShareClient("DefaultEndpointsProtocol=https;AccountName=sacpquat;AccountKey=xYa/oTsWPzkNUgdTdlefU2Y5nwowmvht/cmpDPKqsMpouHggyZ5E1pCxcLUijtKQikF4DWKR+68mlycpsbpHfQ==;EndpointSuffix=core.windows.net", shareName);
ShareDirectoryClient directory = share.GetDirectoryClient("");
ShareFileClient file = directory.GetFileClient(fileName);
return file;
}