简体   繁体   中英

When downloading a large file with c#, the file is corrupted

I have no problems with files under 200 MB, but when I download a large file, the hash values are corrupted.

That's why I try to download large files by dividing them into parts, but at the end of the work, the hash values are corrupted again.

Is there any other way for this or what am I doing wrong in the code below

void getdownload(string url, string fileName, string destinationPath, long totalSize)
{
    List<Tuple<string, string, long>> parts = new List<Tuple<string, string, long>>();
    long partSize = 100 * 1024 * 1024; // 100MB
    int partCount = (int)(totalSize / partSize);
    if (totalSize % partSize > 0)
        partCount++;

    for (int i = 0; i < partCount; i++)
    {
        long start = i * partSize;
        long end = (i + 1) * partSize - 1;
        if (end > totalSize)
            end = totalSize;

        string partFileName = string.Format("{0}.part{1}", fileName, i);
        string partFilePath = Path.Combine(destinationPath, partFileName);
        string urlx = string.Format("{0}?start={1}&end={2}", url, start, end);
        parts.Add(new Tuple<string, string, long>(urlx, partFilePath, end));
    }

    using (WebClient client = new WebClient())
    {
        int currentPart = 0;
        client.DownloadProgressChanged += (s, e) =>
        {
            var totalSize = e.TotalBytesToReceive;
            DownloadChanged(e, parts[0].Item3);
        };
        client.DownloadFileCompleted += (s, e) =>
        {
            if (e.Error != null)
            {
                Console.WriteLine("Error while downloading part {0}: {1}", currentPart, e.Error.Message);
            }
            else if (e.Cancelled)
            {
                Console.WriteLine("Download of part {0} was cancelled", currentPart);
            }
            else
            {
                Console.WriteLine("Part {0} was downloaded successfully", currentPart);
                MessageBox.Show(currentPart.ToString());
                currentPart++;
                if (currentPart < parts.Count)
                {
                    MessageBox.Show(currentPart.ToString());
                    client.DownloadFileAsync(new Uri(parts[currentPart].Item1), parts[0].Item2);
                    client.DownloadFileCompleted += DownloadFileCompleted;
                }
            }
        };
        client.DownloadFileAsync(new Uri(parts[0].Item1), parts[0].Item2);
        client.DownloadFileCompleted += DownloadFileCompleted;
        MessageBox.Show(parts[0].Item1.ToString());
    }
}

I tried fixing my download speed. I said the file on the server might be corrupted, but every time I faced the same problem, the file on the server is intact.

There could be Couple of reasons:

As you explained above the file might be getting corrupted during the writing process to the disk. This can happen if there are errors or interruptions while writing the file to the disk.

The code is writing multiple parts of the file to the same file, which can lead to corrupted file as the parts are not being concatenated together in a correct way.

The DownloadFileCompleted event is being added multiple times and that could cause issues with the download process.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM