The next generation of the Teknik Services. Written in ASP.NET. https://www.teknik.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ResponseHelper.cs 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Web;
  8. namespace Teknik.Utilities
  9. {
  10. public static class ResponseHelper
  11. {
  12. public static void StreamToOutput(HttpResponseBase response, bool flush, Stream stream, int length, int chunkSize)
  13. {
  14. try
  15. {
  16. int processedBytes = 0;
  17. byte[] buffer = new byte[chunkSize];
  18. int bytesRemaining = length;
  19. int bytesToRead = chunkSize;
  20. do
  21. {
  22. // Flush the response
  23. if (flush)
  24. {
  25. //response.OutputStream.Write(buffer, 0, 1);
  26. response.Flush();
  27. }
  28. if (chunkSize > bytesRemaining)
  29. {
  30. bytesToRead = bytesRemaining;
  31. }
  32. processedBytes = stream.Read(buffer, 0, bytesToRead);
  33. if (processedBytes > 0)
  34. {
  35. response.OutputStream.Write(buffer, 0, processedBytes);
  36. // Clear the buffer
  37. Array.Clear(buffer, 0, chunkSize);
  38. }
  39. bytesRemaining -= processedBytes;
  40. }
  41. while (processedBytes > 0 && bytesRemaining > 0);
  42. }
  43. catch (Exception ex)
  44. {
  45. // Don't worry about it. Just leave
  46. }
  47. finally
  48. {
  49. // dispose of file stream
  50. if (stream != null)
  51. {
  52. stream.Dispose();
  53. }
  54. }
  55. }
  56. }
  57. }