// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.AspNetCore.Mvc.Internal;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Net.Http.Headers;
namespace Microsoft.AspNetCore.Mvc
{
///
/// Represents an that when executed will
/// write a binary file to the response.
///
public class FileContentResult : FileResult
{
private byte[] _fileContents;
///
/// Creates a new instance with
/// the provided and the
/// provided .
///
/// The bytes that represent the file contents.
/// The Content-Type header of the response.
public FileContentResult(byte[] fileContents, string contentType)
: this(fileContents, MediaTypeHeaderValue.Parse(contentType))
{
if (fileContents == null)
{
throw new ArgumentNullException(nameof(fileContents));
}
}
///
/// Creates a new instance with
/// the provided and the
/// provided .
///
/// The bytes that represent the file contents.
/// The Content-Type header of the response.
public FileContentResult(byte[] fileContents, MediaTypeHeaderValue contentType)
: base(contentType?.ToString())
{
if (fileContents == null)
{
throw new ArgumentNullException(nameof(fileContents));
}
FileContents = fileContents;
}
///
/// Gets or sets the file contents.
///
public byte[] FileContents
{
get => _fileContents;
set
{
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}
_fileContents = value;
}
}
///
public override Task ExecuteResultAsync(ActionContext context)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
var executor = context.HttpContext.RequestServices.GetRequiredService>();
return executor.ExecuteAsync(context, this);
}
}
}