2018-06-15 02:57:03 +02:00
|
|
|
using Microsoft.AspNetCore.Http;
|
2018-01-27 10:18:28 +01:00
|
|
|
using System;
|
2017-02-10 07:34:50 +01:00
|
|
|
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
|
|
|
|
{
|
2022-05-26 08:30:14 +02:00
|
|
|
public async static Task StreamToOutput(HttpResponse response, Stream stream, int length, int chunkSize)
|
2017-02-10 07:34:50 +01:00
|
|
|
{
|
2022-05-22 03:55:22 +02:00
|
|
|
var bufferSize = chunkSize;
|
|
|
|
if (length < chunkSize)
|
|
|
|
bufferSize = length;
|
2022-05-28 08:03:57 +02:00
|
|
|
var pooledArray = new PooledArray(bufferSize);
|
|
|
|
|
|
|
|
response.RegisterForDispose(pooledArray);
|
2017-02-10 07:34:50 +01:00
|
|
|
try
|
|
|
|
{
|
2022-05-22 04:19:38 +02:00
|
|
|
int processedBytes;
|
2017-02-10 07:34:50 +01:00
|
|
|
do
|
|
|
|
{
|
2022-05-29 23:21:24 +02:00
|
|
|
processedBytes = stream.Read(pooledArray.Array, 0, pooledArray.Length);
|
2017-02-10 07:34:50 +01:00
|
|
|
if (processedBytes > 0)
|
|
|
|
{
|
2022-05-28 08:03:57 +02:00
|
|
|
await response.Body.WriteAsync(pooledArray.Array, 0, processedBytes);
|
2017-04-09 09:16:24 +02:00
|
|
|
|
2022-05-26 08:30:14 +02:00
|
|
|
await response.Body.FlushAsync();
|
2017-02-10 07:34:50 +01:00
|
|
|
}
|
|
|
|
}
|
2022-05-21 18:29:24 +02:00
|
|
|
while (processedBytes > 0);
|
2017-02-10 07:34:50 +01:00
|
|
|
}
|
2022-05-22 04:19:38 +02:00
|
|
|
catch (Exception ex)
|
2017-04-13 19:54:19 +02:00
|
|
|
{
|
|
|
|
// Don't worry about it. Just leave
|
|
|
|
}
|
2017-02-10 07:34:50 +01:00
|
|
|
finally
|
|
|
|
{
|
2022-05-26 08:30:14 +02:00
|
|
|
await response.Body.FlushAsync();
|
2022-05-21 18:29:24 +02:00
|
|
|
|
2017-02-10 07:34:50 +01:00
|
|
|
// dispose of file stream
|
2022-05-26 08:30:14 +02:00
|
|
|
//stream?.Dispose();
|
2017-02-10 07:34:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|