From ba5fe60cf598ddc4a2e1f91e0f2f02fd982b3b96 Mon Sep 17 00:00:00 2001 From: David Fowler Date: Thu, 31 Dec 2015 16:17:53 -0800 Subject: [PATCH] Revert "Change PhysicalFileResult and VirtualFileResult to use SendFileAsync" This reverts commit 5aa2e0630597abe28b86665b26f67f63d7150245. --- .../PhysicalFileResult.cs | 45 ++++++++++++++++++- .../VirtualFileResult.cs | 9 +++- 2 files changed, 50 insertions(+), 4 deletions(-) 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 @@ public string FileName } /// - 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 @@ protected override async Task WriteFileAsync(HttpResponse response) 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 {