代码之家  ›  专栏  ›  技术社区  ›  Chris Hayes

为什么文件流向ftpWebRequest馈送数据而不是memoryStream?

  •  2
  • Chris Hayes  · 技术社区  · 15 年前

    我在这里使用的是msdn示例: http://msdn.microsoft.com/en-us/library/system.net.ftpwebrequest.aspx

    我将文件流更改为memorystream,但它不读取字节

    当我把它改回文件流时,它可以正常工作。

    有什么线索吗?

    谢谢

            CompressMemoryStream();
            Stream requestStream = _request.EndGetRequestStream(ar);
            const int bufferLength = 2048;
            byte[] buffer = new byte[bufferLength];
            int count = 0;
            int readBytes = 0;
    
            do
            {
                //MemoryStream _compressedOutStream 
                //is created/filled by 'CompressMemoryStream()'
                readBytes = _compressedOutStream.Read(buffer, 0, bufferLength);
                requestStream.Write(buffer, 0, readBytes);
                count += readBytes;
            }
            while (readBytes != 0);
            requestStream.Close();
            state.Request.BeginGetResponse(
                new AsyncCallback(EndGetResponseCallback),
                state
            );
    
    1 回复  |  直到 15 年前
        1
  •  3
  •   Joe White    15 年前

    有什么价值 readBytes 在循环的第一次迭代中?

    我的第一个猜测是,你犯了和我经常犯的相同的错误:写到一个流,然后忘记在开始阅读之前把它倒回到开头。如果是这样,那么 读取字节 在第一次(也是唯一一次)循环迭代时将为零,因为您处于流的末尾——没有什么可读取的。

    尝试设置 stream.Position = 0 在你开始阅读之前。