Return memory stream to send word doc to browser C#

Using aspose 23.2 to write comments to a document and saving it to a memory stream.

When using the memory stream to save to afilestream everything works fine.

When using the memory stream to write a httpresponse like below things are not working and its causing the file to be corrupted giving me an error message. If I switch to using the byte array and writing that to the response things work fine.
The thing is I want to AVOID loading the entire byte array as the word document might be very large and could crash my application. Is there a way to still send the memory stream as chunks while avoiding loading the whole byte array to memory?

using (Stream s = data.DownloadableObject.OpenStream())
{
    int bytesRead;
    byte[] buffer = new byte[BufferSize];
    context.Response.BufferOutput = false;
    while ((bytesRead = s.Read(buffer, 0, BufferSize)) > 0 && response.IsClientConnected)
    {
        response.OutputStream.Write(buffer, 0, bytesRead); 
        response.OutputStream.Flush(); 
        if (debugSleepTime > 0) 
            System.Threading.Thread.Sleep(debugSleepTime);
    }
}

@rogarg Could you please create a simple application that will allow us to reproduce the problem on our side? Also, please attach the corrupted document produced on your side. We will check the issue and provide you more information.

Below is the code for how we read from memory stream and return to HttpResponse

using (Stream s = data.DownloadableObject.OpenStream())
{
    // bufferSize = 131072
    int bytesRead;
    byte[] buffer = new byte[BufferSize];
    context.Response.BufferOutput = false;
    while ((bytesRead = s.Read(buffer, 0, BufferSize)) > 0 && response.IsClientConnected)
    {
        response.OutputStream.Write(buffer, 0, bytesRead);
        response.OutputStream.Flush();
        if (debugSleepTime > 0) System.Threading.Thread.Sleep(debugSleepTime);
    }
}
var doc = new Document(stream);

Log.Info("Document successfully loaded from stream");

doc.GetChildNodes(NodeType.Comment, true).Clear();

Log.Info("All comments removed successfully.");

AddCommentsToRegion(doc, comments);

var ms = new MemoryStream();

doc.Save(stream: ms, SaveFormat.Docx);

Log.Info($"Successfully saved {comments.Count} comments to document");

ms.Seek(StreamBeginningPosition, SeekOrigin.Begin);
return ms;

Attached blow is the file.
TEST (2) (8).docx (12.1 KB)

e

@rogarg Please try suing the following code to copy document stream to response output stream:

/// <summary>
/// Copies from the current position in src stream till the end.
/// Copies into the current position in dst stream.
/// </summary>
public static void CopyStream(Stream srcStream, Stream dstStream)
{
    if (srcStream == null)
        throw new ArgumentNullException("srcStream");
    if (dstStream == null)
        throw new ArgumentNullException("dstStream");

    byte[] buf = new byte[65536];
    while (true)
    {
        int bytesRead = srcStream.Read(buf, 0, buf.Length);
        if (bytesRead <= 0)
            break;
        else
            dstStream.Write(buf, 0, bytesRead);
    }
}
using (Stream s = data.DownloadableObject.OpenStream())
{
    CopyStream(s, response.OutputStream);
    response.OutputStream.Flush();
}

Also, you can simply use Document.Save overload to save the document to HttpResponse:
https://docs.aspose.com/words/net/save-a-document/#sending-a-document-to-a-client-browser