English 中文(简体)
下载档案。 网络问题导致腐败案件
原标题:Downloading files. Network problems causes corrupt files

我的申请使用<代码>下载了大量档案。 HttpWebRequest -> WebResponse -> Petro -> fileStream。 见下文守则。

根据这一设想,我们总是得到腐败的档案:

  1. Start download.
  2. Unplug cable or click to pause download process.
  3. Close and open the application.
  4. Start download (it starts from the interruption point).
  5. Wait full file is downloaded.

问题:下载的档案被腐蚀。

我确信这是一个共同的问题,但我没有这样做。 请提出咨询意见。 原因何在?

public class Downloader
{
    int StartPosition { get; set; }
    int EndPosition { get; set; }
    bool Cancelling { get; set; }

    void Download(string[] args)
    {
        string uri = @"http://www.example.com/hugefile.zip";
        string localFile = @"c:hugefile.zip";
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
        request.AddRange(this.StartPosition);
        WebResponse response = request.GetResponse();
        Stream inputStream = response.GetResponseStream();

        using (FileStream fileStream = new FileStream(localFile, FileMode.Open, FileAccess.Write))
        {
            int buffSize = 8192;
            byte[] buffer = new byte[buffSize];
            int readSize;

            do
            {
                // reads the buffer from input stream
                readSize = inputStream.Read(buffer, 0, buffSize);

                fileStream.Position = this.StartPosition;
                fileStream.Write(buffer, 0, (int)readSize);
                fileStream.Flush();

                // increase the start position
                this.StartPosition += readSize;

                // check if the stream has reached its end
                if (this.EndPosition > 0 && this.StartPosition >= this.EndPosition)
                {
                    this.StartPosition = this.EndPosition;
                    break;
                }

                // check if the user have requested to pause the download
                if (this.Cancelling)
                {
                    break;
                }
            }
            while (readSize > 0);
        }
    }
}
最佳回答

为了解决这一难题,我建议进行比较,以确定分歧是什么。 下载的一节是否缺失? 它是否有重复的章节,或者是否有不正确的章节?

问题回答

“AddRange()”电话是错误的,你想要通过负面价值,以便你拿到剩余档案。 根据MSDN图书馆的文章:

If range is positive, the range is from the start of the data to range.
If range is negative, the range is from range to the end of the data.

我看不出,“结束政治”是如何开始的,也可能是错误的。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...