简体   繁体   中英

Appending Memorystream to existing Memorystream

Im having trouble appending the data in a MemoryStream to other Memorystream allready created an full of data . I'm doing this to create an excel from two different sources.

First I create the father memorystream:

MemoryStream obj_stream = new MemoryStream(100000000);
var tempFile = System.IO.Path.Combine(System.IO.Path.GetTempPath(), Guid.NewGuid() + ".xls");
obj_stream.Write(File.ReadAllBytes(tempFile), 0, File.ReadAllBytes(tempFile).Length);
File.Delete(tempFile);

Then I create the son memorystream from a compositeLink.ExportToXls

compositeLink.CreatePageForEachLink();
compositeLink.PrintingSystemBase.XlSheetCreated += PrintingSystemBase_XlSheetCreated;

using (MemoryStream stream = new MemoryStream(100000000))
{
    compositeLink.ExportToXls(stream, new XlsExportOptions() { ExportMode = XlsExportMode.SingleFilePageByPage });
    obj_stream.Write(stream.ToArray(), 0, stream.ToArray().Length);
}

Finally I write the stream to a Page.Response

if (Page == null || Page.Response == null)
    return;
string disposition = saveAsFile ? "attachment" : "inline";
Page.Response.Clear();
Page.Response.Buffer = false;
Page.Response.AppendHeader("Content-Type", string.Format("application/{0}", fileFormat));
Page.Response.AppendHeader("Content-Transfer-Encoding", "binary");
Page.Response.AppendHeader("Content-Disposition", string.Format("{0}; filename={1}.{2}", disposition, fileName, fileFormat));
Page.Response.BinaryWrite(obj_stream.ToArray());
Page.Response.End();

What is happening I believe is that either one or the other gets overwritten by the other, depending on which one I do the "nameOfTheStream".write()

The second time time you write to obj_stream , do not start writing from 0 index (specified in second parameter of Write method) again. There is some data in there already.

obj_stream.Write(stream.ToArray(), obj_stream.ToArray().Length, stream.ToArray().Length);

Method details here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM