openWrite 的流在关闭之前不会写入

发布于 2024-11-25 05:56:14 字数 802 浏览 2 评论 0原文

我有一个使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

无戏配角 2024-12-02 05:56:14

我认为你的问题是因为你的最后一个块可能不是你期望的完整长度(dataChunkSize)。另外,我会添加刷新来强制每次写入,然后如果需要的话(你不确定刷新是否会起作用)。尝试将 for 循环内容更改为此...

byte[] data = new byte[dataChunkSize];
int bytesRead = reader.Read(data, 0, dataChunkSize);
writer.Write(data, 0, bytesRead);
writer.Flush();

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...

byte[] data = new byte[dataChunkSize];
int bytesRead = reader.Read(data, 0, dataChunkSize);
writer.Write(data, 0, bytesRead);
writer.Flush();
自由范儿 2024-12-02 05:56:14

我猜写入是被缓冲的。在缓冲区已满或写入器关闭之前它不会写入。

I guess the write is buffered. It won't write until the buffer is full or the writer is closed.

带刺的爱情 2024-12-02 05:56:14

WebClient 可以使用内部缓冲流(网络等)。

还有关于读者。可以少读点。
所以更好用

    int amountRead = reader.Read(data, 0, dataChunkSize);
    writer.Write(data, 0, amountRead); 
    i += amountRead;

WebClient may use inner buffered stream (network etc.).

And about the reader. It can read less.
So better use

    int amountRead = reader.Read(data, 0, dataChunkSize);
    writer.Write(data, 0, amountRead); 
    i += amountRead;
猥琐帝 2024-12-02 05:56:14

如果你想在关闭之前写入,你需要调用stream.Flush()方法

You will need to call the stream.Flush() method if you want to write before closing

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文