0

我的问题是什么?当我将流写回网络时,打开文件,它包含一些内容,但格式不正确,还有一些内容丢失。

我是否因逻辑错误而丢失数据?

注意:下面的 readstream 和 writestream 模拟了服务将要填写的内容。我将接收要从服务中读取的流。我需要将该流写回。

        MemoryStream writeStream = new MemoryStream();
        byte[] buffer = new byte[256];
        OrderDocument doc = new OrderDocument();
        doc.Format = "xml";
        doc.DocumentId = "5555555";
        doc.Aid = "ZZ";
        doc.PrimaryServerPort = "PORT";
        MemoryStream readStream = new MemoryStream(doc.GetDocument());
        while (readStream != null && readStream.Read(buffer, 0, buffer.Length) > 0)
        {
            writeStream.Write(buffer, 0, buffer.Length);
        }
        writeStream.Flush();
        writeStream.Position = 0;
        Response.Buffer = true;
        Response.Clear();
        Response.ClearContent();
        Response.ClearHeaders();

        Response.ContentType = "text/xml";
        Response.ClearHeaders();
        Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.xml", doc.DocumentId));
        Response.AddHeader("Content-Length", writeStream.Length.ToString());
        Response.BinaryWrite(writeStream.ToArray());
        Response.End();
4

1 回答 1

1

我是否因逻辑错误而丢失数据?

可能是的,您可以尝试稍微简化您的代码。我真的没有看到这里需要多个内存流:

OrderDocument doc = new OrderDocument();
doc.Format = "xml";
doc.DocumentId = "5555555";
doc.Aid = "ZZ";
doc.PrimaryServerPort = "PORT";
byte[] buffer = doc.GetDocument();

Response.Buffer = true;
Response.Clear();
Response.ClearHeaders();
Response.ContentType = "text/xml";
Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.xml", doc.DocumentId));
Response.OutputStream.Write(buffer, 0, buffer.Length);
于 2012-06-26T11:58:54.767 回答