Phillip Davis
Phillip Davis

Reputation: 325

Azure Storage - Downloading Large Files with MVC & SharedAccessFilePolicy

I'm currently downloading files from our website using the simple

    public ActionResult Download() {
        return File(@"D:\Company\Installs\Download.exe", "application/octet-stream", "Download.exe");
    }

This works great, and when the user clicks on the file, they see a % progress as the files are downloading.

We are now moving to Azure File Storage and I'm trying to simulate the same process but with CloudStorageAccount. Overall, it works very well, but I want the same user experience for the user. I think I'm very close to solving this, but the final 'return' isn't giving me a valid ActionResult.

    public async Task<ActionResult> Download() {
        var fileName = "Download.exe";

        var storageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;....");

        var fileClient = storageAccount.CreateCloudFileClient();

        var share = fileClient.GetShareReference("downloads");

        if (!await share.ExistsAsync()) {
            return new EmptyResult();
        }

        var rootDir = share.GetRootDirectoryReference();

        var cloudFile = rootDir.GetFileReference(fileName);

        if (!await cloudFile.ExistsAsync()) {
            return new EmptyResult();
        }

        var policy = new SharedAccessFilePolicy
        {
            Permissions = SharedAccessFilePermissions.Read,
            SharedAccessStartTime = DateTime.UtcNow.AddMinutes(-15),
            SharedAccessExpiryTime = DateTime.UtcNow.AddHours(1)
        };

        var url = cloudFile.Uri.AbsoluteUri + cloudFile.GetSharedAccessSignature(policy);

        // return File(url, "application/octet-stream", fileName);
        return Redirect(url);
    }

The URL returned can be copy/pasted into a browser and the file correctly downloads. But when it's being returned as a FilePathResult, I get ERR_INVALID_RESPONSE, and strangely, the method is called 3 times. Weird.

Can someone help me with the final line of code?

Thanks in advance!

Upvotes: 0

Views: 505

Answers (1)

Phillip Davis
Phillip Davis

Reputation: 325

I finally got all of this working now. The final change to make it work was

return Redirect(url);

Upvotes: 2

Related Questions