使用wcf恢复和上传文件

发布于 2024-11-14 10:41:41 字数 828 浏览 3 评论 0原文

我正在构建一个应用程序,使用 wcf 将文件从客户端上传到服务器,并且我正在尝试实现一种在连接失败时恢复上传的方法。假设我有一个 500 KB 的文件,上传开始但连接失败,只复制了 100 KB。当我恢复时,服务器应用程序知道它有 100 KB。

当我读取用这样的代码传输的流时:

while ((bytes = stream.Read(buffer, 0, bufferSize)) > 0)
  1. 0(偏移量)是我开始读取的字节吗?
  2. 如果是的话,我应该从100开始读吗?像这样:

    while ((bytes = stream.Read(buffer, 100, bufferSize)) > 0)

然后将字节附加到现有的不完整文件?

这是我的代码:

using (FileStream fs = new FileStream(@"C:\" + name, FileMode.Append))
{
    int bufferSize = 4 * 1024; // 4KB buffer
    byte[] buffer = new byte[bufferSize];
    int bytes;

    while ((bytes = stream.Read(buffer, 100, bufferSize)) > 0)
    {
        fs.Write(buffer, 100, bytes);
        fs.Flush();
    }
    stream.Close();
    fs.Close();
}

我必须做出哪些更改?

I'm building an application to upload files from clients to a server using wcf and I'm trying to implement a way to resume the upload if connection fails. Suppose I have a 500 KB file, the upload begins and connection fails, only 100 KB were copied. When I resume, the server application knows it has 100 KB.

When I read the stream transmitted with code like this:

while ((bytes = stream.Read(buffer, 0, bufferSize)) > 0)
  1. Is that 0 (offset) the byte where I begin to read??
  2. If so, should I begin to read from 100?? something like:

    while ((bytes = stream.Read(buffer, 100, bufferSize)) > 0)

and then append bytes to the existing incomplete file??

This is my code:

using (FileStream fs = new FileStream(@"C:\" + name, FileMode.Append))
{
    int bufferSize = 4 * 1024; // 4KB buffer
    byte[] buffer = new byte[bufferSize];
    int bytes;

    while ((bytes = stream.Read(buffer, 100, bufferSize)) > 0)
    {
        fs.Write(buffer, 100, bytes);
        fs.Flush();
    }
    stream.Close();
    fs.Close();
}

What changes do I have to make??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

哽咽笑 2024-11-21 10:41:41

不,偏移量指定缓冲区中开始复制从流中读取的字节的位置。

您可能想做的是“寻求”。 Stream.Seek(100,SeekOrigin.Begin);
这具有“跳过”流中前 100 个字节的效果。
那时,您将想要开始阅读......

如果您执行此简历操作,那么我建议使用 CRC 或 MD5 或其他东西,在完成后验证文件的内容。

No, the offset specifies where in the buffer to begin to COPY the bytes that are read from the stream.

What you may want to do is Seek. Stream.Seek(100, SeekOrigin.Begin);
This has the effect of "skipping" the first 100 bytes in the stream.
At that point, you will want to begin reading....

If you do this resume thing, then I suggest using a CRC or MD5 or something, to verify the contents of the file after completion.

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