Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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