openWrite 的流在关闭之前不会写入
我有一个使用 WebClient.OpenWrite 调用打开的流编写器。对于这种简化的情况,假设读取器正在读取 dataChunkSize 的倍数。
using (Stream writer = myWebClient.OpenWrite(myURIString)
{
using (FileStream reader = new FileStream(myFileName, FileMode.Open, FileAccess.Read)
{
for(int i = 0; i < reader.Length; i += dataChunkSize)
{
byte[] data = new byte[dataChunkSize];
reader.Read(data, 0, dataChunkSize);
writer.Write(data, 0, dataChunkSize);
}
reader.Close();
reader.Dispose();
}
writer.Close();
writer.Dispose();
}
我的数据大小是2个dataChunkSizes。但是,在调用 writer.Close() 之前,它不会发送任何数据(没有接收到数据),此时它只发送第一个 dataChunkSize 数据...第二个 dataChunkSize 数据永远不会发送。
如何在每次 Write 调用后发送它?我尝试添加 writer.Flush() 但这没有帮助。
谢谢。
I have a stream writer that opens using a WebClient.OpenWrite call. For this simplified case, assume that reader is reading a multiple of dataChunkSize.
using (Stream writer = myWebClient.OpenWrite(myURIString)
{
using (FileStream reader = new FileStream(myFileName, FileMode.Open, FileAccess.Read)
{
for(int i = 0; i < reader.Length; i += dataChunkSize)
{
byte[] data = new byte[dataChunkSize];
reader.Read(data, 0, dataChunkSize);
writer.Write(data, 0, dataChunkSize);
}
reader.Close();
reader.Dispose();
}
writer.Close();
writer.Dispose();
}
My data is the size of 2 dataChunkSizes. However, it does not send any data (no data is received) until the writer.Close() call is called, where it only sends the first dataChunkSize worth of data...the second dataChunkSize of data is never sent.
How can I get it to send after every Write call? I tried adding writer.Flush() but this did not help.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为你的问题是因为你的最后一个块可能不是你期望的完整长度(dataChunkSize)。另外,我会添加刷新来强制每次写入,然后如果需要的话(你不确定刷新是否会起作用)。尝试将 for 循环内容更改为此...
I think your issue is because your last chunk is perhaps not the full length you expect (dataChunkSize). Also, I would add Flush to force each write there and then if needed (thou I am not sure if the flush will work). Try changing you for loop contents to this...
我猜写入是被缓冲的。在缓冲区已满或写入器关闭之前它不会写入。
I guess the write is buffered. It won't write until the buffer is full or the writer is closed.
WebClient 可以使用内部缓冲流(网络等)。
还有关于读者。可以少读点。
所以更好用
WebClient may use inner buffered stream (network etc.).
And about the reader. It can read less.
So better use
如果你想在关闭之前写入,你需要调用stream.Flush()方法
You will need to call the stream.Flush() method if you want to write before closing