83 lines
2.9 KiB
C#
83 lines
2.9 KiB
C#
// 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
|
|
{
|
|
/// <summary>
|
|
/// A <see cref="FileResult"/> on execution will write a file from disk to the response
|
|
/// using mechanisms provided by the host.
|
|
/// </summary>
|
|
public class PhysicalFileResult : FileResult
|
|
{
|
|
private string _fileName;
|
|
|
|
/// <summary>
|
|
/// Creates a new <see cref="PhysicalFileResult"/> instance with
|
|
/// the provided <paramref name="fileName"/> and the provided <paramref name="contentType"/>.
|
|
/// </summary>
|
|
/// <param name="fileName">The path to the file. The path must be an absolute path.</param>
|
|
/// <param name="contentType">The Content-Type header of the response.</param>
|
|
public PhysicalFileResult(string fileName, string contentType)
|
|
: this(fileName, MediaTypeHeaderValue.Parse(contentType))
|
|
{
|
|
if (fileName == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(fileName));
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates a new <see cref="PhysicalFileResult"/> instance with
|
|
/// the provided <paramref name="fileName"/> and the provided <paramref name="contentType"/>.
|
|
/// </summary>
|
|
/// <param name="fileName">The path to the file. The path must be an absolute path.</param>
|
|
/// <param name="contentType">The Content-Type header of the response.</param>
|
|
public PhysicalFileResult(string fileName, MediaTypeHeaderValue contentType)
|
|
: base(contentType?.ToString())
|
|
{
|
|
if (fileName == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(fileName));
|
|
}
|
|
|
|
FileName = fileName;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets or sets the path to the file that will be sent back as the response.
|
|
/// </summary>
|
|
public string FileName
|
|
{
|
|
get => _fileName;
|
|
set
|
|
{
|
|
if (value == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(value));
|
|
}
|
|
|
|
_fileName = value;
|
|
}
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public override Task ExecuteResultAsync(ActionContext context)
|
|
{
|
|
if (context == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(context));
|
|
}
|
|
|
|
var executor = context.HttpContext.RequestServices.GetRequiredService<IActionResultExecutor<PhysicalFileResult>>();
|
|
return executor.ExecuteAsync(context, this);
|
|
}
|
|
}
|
|
}
|