diff --git a/src/Microsoft.AspNet.Mvc.Core/PhysicalFileResult.cs b/src/Microsoft.AspNet.Mvc.Core/PhysicalFileResult.cs index 0752dd61c6..6c343d3fef 100644 --- a/src/Microsoft.AspNet.Mvc.Core/PhysicalFileResult.cs +++ b/src/Microsoft.AspNet.Mvc.Core/PhysicalFileResult.cs @@ -3,8 +3,10 @@ using System; using System.IO; +using System.Threading; using System.Threading.Tasks; using Microsoft.AspNet.Http; +using Microsoft.AspNet.Http.Features; using Microsoft.AspNet.Mvc.Core; using Microsoft.Net.Http.Headers; @@ -16,6 +18,7 @@ namespace Microsoft.AspNet.Mvc /// public class PhysicalFileResult : FileResult { + private const int DefaultBufferSize = 0x1000; private string _fileName; /// @@ -80,14 +83,52 @@ namespace Microsoft.AspNet.Mvc } /// - protected override Task WriteFileAsync(HttpResponse response) + protected override async Task WriteFileAsync(HttpResponse response) { if (!Path.IsPathRooted(FileName)) { throw new NotSupportedException(Resources.FormatFileResult_PathNotRooted(FileName)); } - return response.SendFileAsync(FileName); + var sendFile = response.HttpContext.Features.Get(); + if (sendFile != null) + { + await sendFile.SendFileAsync( + FileName, + offset: 0, + length: null, + cancellation: default(CancellationToken)); + } + else + { + var fileStream = GetFileStream(FileName); + + using (fileStream) + { + await fileStream.CopyToAsync(response.Body, DefaultBufferSize); + } + } + } + + /// + /// Returns for the specified . + /// + /// The path for which the is needed. + /// for the specified . + protected virtual Stream GetFileStream(string path) + { + if (path == null) + { + throw new ArgumentNullException(nameof(path)); + } + + return new FileStream( + path, + FileMode.Open, + FileAccess.Read, + FileShare.ReadWrite, + DefaultBufferSize, + FileOptions.Asynchronous | FileOptions.SequentialScan); } } } diff --git a/src/Microsoft.AspNet.Mvc.Core/VirtualFileResult.cs b/src/Microsoft.AspNet.Mvc.Core/VirtualFileResult.cs index 96466e7342..5dd5c302a5 100644 --- a/src/Microsoft.AspNet.Mvc.Core/VirtualFileResult.cs +++ b/src/Microsoft.AspNet.Mvc.Core/VirtualFileResult.cs @@ -106,9 +106,14 @@ namespace Microsoft.AspNet.Mvc if (fileInfo.Exists) { var physicalPath = fileInfo.PhysicalPath; - if (!string.IsNullOrEmpty(physicalPath)) + var sendFile = response.HttpContext.Features.Get(); + if (sendFile != null && !string.IsNullOrEmpty(physicalPath)) { - await response.SendFileAsync(physicalPath); + await sendFile.SendFileAsync( + physicalPath, + offset: 0, + length: null, + cancellation: default(CancellationToken)); } else {