diff --git a/products/ASC.Files/Core/HttpHandlers/FileHandler.ashx.cs b/products/ASC.Files/Core/HttpHandlers/FileHandler.ashx.cs index 1a293a4b65a..e50d1f428aa 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); + await SendStreamByChunksAsync(context, length, filename, readStream, false); } 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 = await SendStreamByChunksAsync(context, length, title, fileStream, flushed); } 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 = await SendStreamByChunksAsync(context, length, title, fileStream, flushed); } } 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) + private async Task SendStreamByChunksAsync(HttpContext context, long toRead, string title, Stream fileStream, bool flushed) { context.Response.Headers.Add("Connection", "Keep-Alive"); context.Response.ContentLength = toRead; @@ -618,7 +618,6 @@ 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); @@ -781,13 +780,15 @@ private async Task StreamFileAsync(HttpContext context, T id) return; } - long offset = 0; - var length = ProcessRangeHeader(context, file.ContentLength, ref offset); + context.Response.Headers.Add("Content-Disposition", ContentDispositionUtil.GetHeaderValue(file.Title)); + context.Response.ContentType = MimeMapping.GetMimeMapping(file.Title); await using var stream = await fileDao.GetFileStreamAsync(file); - stream.Seek(offset, SeekOrigin.Begin); - - await SendStreamByChunksAsync(context, length, file.Title, stream); + context.Response.Headers.Add("Content-Length", + stream.CanSeek + ? stream.Length.ToString(CultureInfo.InvariantCulture) + : file.ContentLength.ToString(CultureInfo.InvariantCulture)); + await stream.CopyToAsync(context.Response.Body); } catch (Exception ex) {