This repository has been archived by the owner on Dec 14, 2018. It is now read-only.
/
PhysicalFileResult.cs
82 lines (73 loc) · 2.91 KB
/
PhysicalFileResult.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// 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);
}
}
}