Skip to content

Instantly share code, notes, and snippets.

@darrenferguson
Created June 11, 2020 07:07
Show Gist options
  • Save darrenferguson/efda4d68d535c9e4a67963bb847c042e to your computer and use it in GitHub Desktop.
Save darrenferguson/efda4d68d535c9e4a67963bb847c042e to your computer and use it in GitHub Desktop.
public ActionResult GetFileStreamResult(UrlInformation urlInformation, HttpContext context)
{
string url = urlInformation.Path;
string connection = ConfigurationManager.AppSettings["AzureBlobFileSystem.ConnectionString:media"];
string containerName = ConfigurationManager.AppSettings["AzureBlobFileSystem.ContainerName:media"];
CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(connection);
var client = cloudStorageAccount.CreateCloudBlobClient();
var container = client.GetContainerReference(containerName);
var blob = container.GetBlockBlobReference(url.Replace("/media/", string.Empty));
if (!blob.Exists())
{
throw new FileNotFoundException(url);
}
context.Response.Clear();
context.Response.Expires = -1;
ContentDisposition cd = new ContentDisposition
{
FileName = urlInformation.FileName,
Inline = true // false = prompt the user for downloading; true = browser to try to show the file inline
};
if (context.Response.Headers["content-disposition"] != null)
{
context.Response.Headers.Remove("content-disposition");
}
context.Response.Headers["content-disposition"] = cd.ToString();
Stream blobStream = blob.OpenRead();
context.Response.Headers.Add("content-length", blobStream.Length.ToString());
return new FileStreamResult(blobStream, urlInformation.MimeType);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment