Kushan Sameera
Kushan Sameera

Reputation: 229

Download file in azure storage using ASP.Net core2

I have web application and user should be able to download files which are stored in the azure storage.

public class FileDownloader
{
    private string constring = "myconnectionstring";

    public async Task download(string fileName, string directoryName)
    {
        try
        {
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(constring);
            CloudFileClient fileClient = storageAccount.CreateCloudFileClient();
            CloudFileShare share = fileClient.GetShareReference("myreference");
            string userName = Environment.UserName;


            if (await share.ExistsAsync())
            {
                // Get a reference to the root directory for the share.
                CloudFileDirectory rootDir = share.GetRootDirectoryReference();

                // Get a reference to the directory we created previously.
                CloudFileDirectory sampleDir = rootDir.GetDirectoryReference(directoryName);

                // Ensure that the directory exists.
                if (await sampleDir.ExistsAsync())
                {
                    // Get a reference to the file we created previously.
                    CloudFile file = sampleDir.GetFileReference(fileName);

                    // Ensure that the file exists.
                    if (await file.ExistsAsync())
                    {

                        await file.DownloadToFileAsync(string.Format("C:/Users/" + userName + "/Downloads/{0}", fileName), FileMode.Create);
                    }
                }
            }

        }
        catch (Exception ex)
        {

        }
    }
}

This is my code.

It works perfectly when I run application in my local environment(localhost) but when I deploy it and host it in the azure, it isn't downloading the files.

Upvotes: 0

Views: 1336

Answers (1)

Baptistin
Baptistin

Reputation: 71

DownloadToFileAsync() : it allows you to download the file in the directory of your application not client.

May be use a stream to download it ?

How to download files from Azure Blob Storage with a Download Link

Upvotes: 1

Related Questions