---
title: "How do you resume a file download that was interrupted halfway?"  
description: "How do you resume a file download that was interrupted halfway?"  
author: "ICSM Computer"  
published: 2025-05-18  
updated: 2025-05-18  
canonical: https://www.mindstick.com/interview/34132/how-do-you-resume-a-file-download-that-was-interrupted-halfway  
category: "c#"  
tags: ["c#", "file handling"]  
reading_time: 4 minutes  

---

# How do you resume a file download that was interrupted halfway?

To **resume a file download that was interrupted halfway**, you need to support **HTTP Range requests** — a mechanism that allows clients to request only a portion of a file. Here's how to implement it in both **client-side** and **server-side** code using C#.

## 1. Client-Side: Resume Download with `HttpClient`

```cs
public async Task ResumeDownloadAsync(string url, string localPath)
{
    long existingLength = 0;
    if (File.Exists(localPath))
        existingLength = new FileInfo(localPath).Length;

    using var handler = new HttpClientHandler() { AllowAutoRedirect = true };
    using var client = new HttpClient(handler);

    // Set Range header
    client.DefaultRequestHeaders.Range = new System.Net.Http.Headers.RangeHeaderValue(existingLength, null);

    using var response = await client.GetAsync(url, HttpCompletionOption.ResponseHeadersRead);

    response.EnsureSuccessStatusCode();

    using var httpStream = await response.Content.ReadAsStreamAsync();
    using var fileStream = new FileStream(localPath, FileMode.Append, FileAccess.Write, FileShare.None);

    await httpStream.CopyToAsync(fileStream);
}
```

**Behavior**:

1. If `localPath` file exists, we calculate how many bytes have already been downloaded.
2. Then we request only the remaining part of the file using `Range: bytes=start-`.

## 2. Server-Side: Support Range Requests (ASP.NET Core Example)

```cs
[HttpGet("download/{filename}")]
public IActionResult DownloadFile(string filename)
{
    var filePath = Path.Combine("files", filename);
    var file = new FileInfo(filePath);
    if (!file.Exists)
        return NotFound();

    var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
    return File(stream, "application/octet-stream", file.Name, enableRangeProcessing: true);
}
```

**Key Point**:

1. `enableRangeProcessing: true` lets ASP.NET Core handle partial requests for you.
2. The framework will respond with HTTP 206 (Partial Content) if a range is requested.

## 3. HTTP Header Behavior

1. When resuming, the server should:
2. Accept `Range: bytes=start-`
3. Respond with `206 Partial Content`
4. Include headers like `Content-Range` and `Accept-Ranges: bytes`

## 4. Things to Watch

1. Some servers do not support range requests. Test for `Accept-Ranges` in response headers.
2. Ensure consistent file sizes and no corruption between resume attempts.
3. Always handle the case where the server returns `200 OK` instead of `206`.

## Answers

### Answer by ICSM Computer

To **resume a file download that was interrupted halfway**, you need to support **HTTP Range requests** — a mechanism that allows clients to request only a portion of a file. Here's how to implement it in both **client-side** and **server-side** code using C#.

## 1. Client-Side: Resume Download with `HttpClient`

```cs
public async Task ResumeDownloadAsync(string url, string localPath)
{
    long existingLength = 0;
    if (File.Exists(localPath))
        existingLength = new FileInfo(localPath).Length;

    using var handler = new HttpClientHandler() { AllowAutoRedirect = true };
    using var client = new HttpClient(handler);

    // Set Range header
    client.DefaultRequestHeaders.Range = new System.Net.Http.Headers.RangeHeaderValue(existingLength, null);

    using var response = await client.GetAsync(url, HttpCompletionOption.ResponseHeadersRead);

    response.EnsureSuccessStatusCode();

    using var httpStream = await response.Content.ReadAsStreamAsync();
    using var fileStream = new FileStream(localPath, FileMode.Append, FileAccess.Write, FileShare.None);

    await httpStream.CopyToAsync(fileStream);
}
```

**Behavior**:

1. If `localPath` file exists, we calculate how many bytes have already been downloaded.
2. Then we request only the remaining part of the file using `Range: bytes=start-`.

## 2. Server-Side: Support Range Requests (ASP.NET Core Example)

```cs
[HttpGet("download/{filename}")]
public IActionResult DownloadFile(string filename)
{
    var filePath = Path.Combine("files", filename);
    var file = new FileInfo(filePath);
    if (!file.Exists)
        return NotFound();

    var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
    return File(stream, "application/octet-stream", file.Name, enableRangeProcessing: true);
}
```

**Key Point**:

1. `enableRangeProcessing: true` lets ASP.NET Core handle partial requests for you.
2. The framework will respond with HTTP 206 (Partial Content) if a range is requested.

## 3. HTTP Header Behavior

1. When resuming, the server should:
2. Accept `Range: bytes=start-`
3. Respond with `206 Partial Content`
4. Include headers like `Content-Range` and `Accept-Ranges: bytes`

## 4. Things to Watch

1. Some servers do not support range requests. Test for `Accept-Ranges` in response headers.
2. Ensure consistent file sizes and no corruption between resume attempts.
3. Always handle the case where the server returns `200 OK` instead of `206`.


---

Original Source: https://www.mindstick.com/interview/34132/how-do-you-resume-a-file-download-that-was-interrupted-halfway

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
