Format error while trying to download excel from Azure File Share using .Net Core

Shetty Gunaprasad (Consultant) 21 Reputation points
2023-06-05T17:39:12.08+00:00

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;
            
        }
Azure Files
Azure Files
An Azure service that offers file shares in the cloud.
1,162 questions
Azure Storage Accounts
Azure Storage Accounts
Globally unique resources that provide access to data management services and serve as the parent namespace for the services.
2,687 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,158 questions
{count} votes

Accepted answer
  1. KarishmaTiwari-MSFT 18,367 Reputation points Microsoft Employee
    2023-06-07T06:25:18.4233333+00:00

    @Shetty Gunaprasad (Consultant) Thanks for posting your query on Microsoft Q&A.

    Please follow the code sample provided here to download files from Azure File share in C# : https://learn.microsoft.com/en-us/dotnet/api/overview/azure/storage.files.shares-readme?view=azure-dotnet#async-apis

    Regarding format error, please ensure that the extension of file being downloaded from Azure File Share matches extension of file that is saved on local disk.

    Let me know the results. Thanks.


    If you have any questions at all, please let me know in the "comments" and I would be happy to help you. Comment is the fastest way of notifying the experts.

    Please don’t forget to Accept Answer and hit Yes for "was this answer helpful" wherever the information provided helps you. This can be beneficial to other community members for remediation for similar issues.

    User's image

    0 comments No comments

0 additional answers

Sort by: Most helpful