/// <summary> /// Sends the given file using the SendFile extension. /// </summary> /// <param name="response"></param> /// <param name="file">The file.</param> /// <param name="offset">The offset in the file.</param> /// <param name="count">The number of bytes to send, or null to send the remainder of the file.</param> /// <param name="cancellationToken"></param> /// <returns></returns> public static async Task SendFileAsync(this HttpResponse response, IFileInfo file, long offset, long?count, CancellationToken cancellationToken = default(CancellationToken)) { if (response == null) { throw new ArgumentNullException(nameof(response)); } if (file == null) { throw new ArgumentNullException(nameof(file)); } CheckRange(offset, count, file.Length); if (string.IsNullOrEmpty(file.PhysicalPath)) { using (var fileContent = file.CreateReadStream()) { if (offset > 0) { fileContent.Seek(offset, SeekOrigin.Begin); } await StreamCopyOperation.CopyToAsync(fileContent, response.Body, count, cancellationToken); } } else { await response.SendFileAsync(file.PhysicalPath, offset, count, cancellationToken); } }
private static async Task SendFileAsyncCore(HttpResponse response, IFileInfo file, long offset, long?count, CancellationToken cancellationToken) { if (string.IsNullOrEmpty(file.PhysicalPath)) { CheckRange(offset, count, file.Length); using var fileContent = file.CreateReadStream(); var useRequestAborted = !cancellationToken.CanBeCanceled; var localCancel = useRequestAborted ? response.HttpContext.RequestAborted : cancellationToken; try { localCancel.ThrowIfCancellationRequested(); if (offset > 0) { fileContent.Seek(offset, SeekOrigin.Begin); } await StreamCopyOperation.CopyToAsync(fileContent, response.Body, count, StreamCopyBufferSize, localCancel); } catch (OperationCanceledException) when(useRequestAborted) { } } else { await response.SendFileAsync(file.PhysicalPath, offset, count, cancellationToken); } }
/// <summary> /// Sends the given file using the SendFile extension. /// </summary> /// <param name="response"></param> /// <param name="file">The file.</param> /// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param> public static Task SendFileAsync(this HttpResponse response, IFileInfo file, CancellationToken cancellationToken = default(CancellationToken)) { if (response == null) { throw new ArgumentNullException(nameof(response)); } if (file == null) { throw new ArgumentNullException(nameof(file)); } return(response.SendFileAsync(file, 0, null, cancellationToken)); }
private static async Task SendFileAsyncCore(HttpResponse response, IFileInfo file, long offset, long?count, CancellationToken cancellationToken) { if (string.IsNullOrEmpty(file.PhysicalPath)) { CheckRange(offset, count, file.Length); using (var fileContent = file.CreateReadStream()) { if (offset > 0) { fileContent.Seek(offset, SeekOrigin.Begin); } await StreamCopyOperation.CopyToAsync(fileContent, response.Body, count, cancellationToken); } } else { await response.SendFileAsync(file.PhysicalPath, offset, count, cancellationToken); } }