UnityWebRequest Resuming Download Progress Issue

Hi everyone,

I’m encountering an issue with resuming downloads using UnityWebRequest in Unity. Despite receiving a 206 Partial Content response from the server, the download progress starts from 0% instead of reflecting the already downloaded portion of the file.

Here’s what I’ve done so far:

  1. Range Header: Set the Range header to request the remaining bytes of the file.
  2. Manual Progress Calculation: Calculated progress manually by adding the existing file length to the downloaded bytes.
  3. Server Response: Verified that the server responds with 206 Partial Content.

Code Snippet:

using UnityEngine;
using UnityEngine.Networking;
using System.IO;
using System.Collections;
using UnityEngine.UI;

public class DownloadManager : MonoBehaviour
{
public string url = “https://example.com/largefile.zip”;
public string savePath = “path/to/save/largefile.zip”;
public Slider progressBar; // UI element to show progress

IEnumerator Start()
{
    yield return StartCoroutine(DownloadFile(url, savePath));
}

IEnumerator DownloadFile(string url, string savePath)
{
    long fileLength = 0;
    if (File.Exists(savePath))
    {
        FileInfo fileInfo = new FileInfo(savePath);
        fileLength = fileInfo.Length;
    }

    UnityWebRequest request = UnityWebRequest.Get(url);
    if (fileLength > 0)
    {
        request.SetRequestHeader("Range", "bytes=" + fileLength + "-");
    }

    request.downloadHandler = new DownloadHandlerFile(savePath, true);
    request.SendWebRequest();

    while (!request.isDone)
    {
        // Calculate progress manually
        long totalBytes = fileLength + request.downloadedBytes;
        long totalFileSize = fileLength + long.Parse(request.GetResponseHeader("Content-Length"));
        float progress = (float)totalBytes / (float)totalFileSize;
        progressBar.value = progress;
        yield return null;
    }

    if (request.result == UnityWebRequest.Result.ConnectionError || request.result == UnityWebRequest.Result.ProtocolError)
    {
        Debug.LogError("Error: " + request.error);
    }
    else if (request.responseCode == 206) // Partial Content
    {
        Debug.Log("Resumed download complete");
    }
    else if (request.responseCode == 200) // OK
    {
        Debug.Log("Download started from the beginning");
    }
    else
    {
        Debug.Log("Download complete");
    }
}

}

UnityWebRequest indicates the progress of the current download, it shows how much of what server is sending been delivered. The fact that you request only a portion of file is irrelevant.
The resume of download is your higher level logic on top. For UWR it is a partial download (which isn’t necessarily mean the beginning was already downloaded) that is appended to the end of existing file (which not necessarily contains the start of the file up to the portion being downloaded).

Can you log the values of request.downloadedBytes and request.GetResponseHeader("Content-Length") to the console and share the values? Your code seems to make sense I wonder if either of these values is not available or doesn’t have the expected value.

request.downloadedbytes this value always been 0 even after 50 percentage file completed what is the reason this is the UWR nature