diff --git a/products/ASC.Files/Core/HttpHandlers/FileHandler.ashx.cs b/products/ASC.Files/Core/HttpHandlers/FileHandler.ashx.cs index e50d1f428aa..1a293a4b65a 100644 --- a/products/ASC.Files/Core/HttpHandlers/FileHandler.ashx.cs +++ b/products/ASC.Files/Core/HttpHandlers/FileHandler.ashx.cs @@ -289,7 +289,7 @@ private async ValueTask BulkDownloadFile(HttpContext context) readStream.Seek(offset, SeekOrigin.Begin); } - await SendStreamByChunksAsync(context, length, filename, readStream, false); + await SendStreamByChunksAsync(context, length, filename, readStream); } await context.Response.Body.FlushAsync(); @@ -490,7 +490,7 @@ private async Task DownloadFile(HttpContext context, T id, bool forView) } } - flushed = await SendStreamByChunksAsync(context, length, title, fileStream, flushed); + flushed = await SendStreamByChunksAsync(context, length, title, fileStream); } else { @@ -513,7 +513,7 @@ private async Task DownloadFile(HttpContext context, T id, bool forView) fileStream.Seek(offset, SeekOrigin.Begin); } - flushed = await SendStreamByChunksAsync(context, length, title, fileStream, flushed); + flushed = await SendStreamByChunksAsync(context, length, title, fileStream); } } catch (ThreadAbortException tae) @@ -609,7 +609,7 @@ private long ProcessRangeHeader(HttpContext context, long fullLength, ref long o return length; } - private async Task SendStreamByChunksAsync(HttpContext context, long toRead, string title, Stream fileStream, bool flushed) + private async Task SendStreamByChunksAsync(HttpContext context, long toRead, string title, Stream fileStream) { context.Response.Headers.Add("Connection", "Keep-Alive"); context.Response.ContentLength = toRead; @@ -618,6 +618,7 @@ private async Task SendStreamByChunksAsync(HttpContext context, long toRea var bufferSize = Convert.ToInt32(Math.Min(32 * 1024, toRead)); // 32KB var buffer = new byte[bufferSize]; + var flushed = false; while (toRead > 0) { var length = await fileStream.ReadAsync(buffer, 0, bufferSize); @@ -780,15 +781,13 @@ private async Task StreamFileAsync(HttpContext context, T id) return; } - context.Response.Headers.Add("Content-Disposition", ContentDispositionUtil.GetHeaderValue(file.Title)); - context.Response.ContentType = MimeMapping.GetMimeMapping(file.Title); + long offset = 0; + var length = ProcessRangeHeader(context, file.ContentLength, ref offset); await using var stream = await fileDao.GetFileStreamAsync(file); - context.Response.Headers.Add("Content-Length", - stream.CanSeek - ? stream.Length.ToString(CultureInfo.InvariantCulture) - : file.ContentLength.ToString(CultureInfo.InvariantCulture)); - await stream.CopyToAsync(context.Response.Body); + stream.Seek(offset, SeekOrigin.Begin); + + await SendStreamByChunksAsync(context, length, file.Title, stream); } catch (Exception ex) {