1
0
mirror of https://git.teknik.io/Teknikode/Teknik.git synced 2023-08-02 14:16:22 +02:00
Teknik/Utilities/ResponseHelper.cs
2022-05-29 14:21:24 -07:00

51 lines
1.4 KiB
C#

using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
namespace Teknik.Utilities
{
public static class ResponseHelper
{
public async static Task StreamToOutput(HttpResponse response, Stream stream, int length, int chunkSize)
{
var bufferSize = chunkSize;
if (length < chunkSize)
bufferSize = length;
var pooledArray = new PooledArray(bufferSize);
response.RegisterForDispose(pooledArray);
try
{
int processedBytes;
do
{
processedBytes = stream.Read(pooledArray.Array, 0, pooledArray.Length);
if (processedBytes > 0)
{
await response.Body.WriteAsync(pooledArray.Array, 0, processedBytes);
await response.Body.FlushAsync();
}
}
while (processedBytes > 0);
}
catch (Exception ex)
{
// Don't worry about it. Just leave
}
finally
{
await response.Body.FlushAsync();
// dispose of file stream
//stream?.Dispose();
}
}
}
}