Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将文件分部分流式传输-不工作_C#_Wcf_Stream - Fatal编程技术网

C# 将文件分部分流式传输-不工作

C# 将文件分部分流式传输-不工作,c#,wcf,stream,C#,Wcf,Stream,我有一个客户机-服务器应用程序,它在transfare模式流中通过wcf进行通信。 当客户端尝试在一次和平中下载该文件时,其工作正常,但当客户端尝试在两次和平中获取整个文件时,下载的文件已损坏,无法打开 客户端代码: public void DownloadPart(Part Part) //Part: Part.From,Part.To -> possitions in the stream from where to begin and when to end reading

我有一个客户机-服务器应用程序,它在transfare模式流中通过wcf进行通信。 当客户端尝试在一次和平中下载该文件时,其工作正常,但当客户端尝试在两次和平中获取整个文件时,下载的文件已损坏,无法打开

客户端代码:

public void DownloadPart(Part Part) //Part: Part.From,Part.To -> possitions in the stream from where to begin and when to end reading
                {
                    int ReadUntilNow = 0;
                    int ReadNow = 0;
                    byte[] Array= new byte[15000];
                    long NeedToDownload = Part.To - Part.From;
                    using (FileStream MyFile = new FileStream(Path_To_Save_The_File, FileMode.Create, FileAccess.Write, FileShare.ReadWrite))
                    {
                        MyFile.Position = Part.From;
                        while (ReadUntilNow < NeedToDownload)
                        {
                            ReadNow = this.SeederInterface.GetBytes(TorrentID, Part.From + ReadUntilNow, ref Array);
                            ReadUntilNow += ReadNow;
                            MyFile.Write(Array, 0, ReadNow);
                        }
                    }
                }

我真的没有希望了,不知道是什么问题。这将覆盖任何现有的输出文件。你有:

using (FileStream MyFile = new FileStream(Path_To_Save_The_File, 
    FileMode.Create, FileAccess.Write, FileShare.ReadWrite))
这将创建一个新文件,或覆盖现有文件

在下一行中,您有:

MyFile.Position = Part.From
这将扩展文件,文件的第一部分将包含垃圾——该空间中磁盘上的任何内容

我认为您需要将open调用中的
模式更改为
FileMode.open或create
,如下所示:

using (FileStream MyFile = new FileStream(Path_To_Save_The_File, 
    FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite))
如果文件已经存在,则将打开该文件。否则它将创建一个新文件


您可能需要确定是否正在下载文件的第一部分(即新文件),并删除任何现有文件(如果是)。否则,您的代码可能会覆盖新文件的第一部分,但不会截断。

这将覆盖任何现有的输出文件。你有:

using (FileStream MyFile = new FileStream(Path_To_Save_The_File, 
    FileMode.Create, FileAccess.Write, FileShare.ReadWrite))
这将创建一个新文件,或覆盖现有文件

在下一行中,您有:

MyFile.Position = Part.From
这将扩展文件,文件的第一部分将包含垃圾——该空间中磁盘上的任何内容

我认为您需要将open调用中的
模式更改为
FileMode.open或create
,如下所示:

using (FileStream MyFile = new FileStream(Path_To_Save_The_File, 
    FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite))
如果文件已经存在,则将打开该文件。否则它将创建一个新文件


您可能需要确定是否正在下载文件的第一部分(即新文件),并删除任何现有文件(如果是)。否则,您的代码可能会覆盖新文件的第一部分,但不会截断。

哇,这是我的错误。。。真不敢相信。非常感谢您抽出时间!我还是不敢相信我没有看到它!再次谢谢你!哇那是我的错。。。真不敢相信。非常感谢您抽出时间!我还是不敢相信我没有看到它!再次谢谢你!