Download big file failed but it gives a success message

Dani_S 4,521 Reputation points
2025-03-27T10:26:59.3466667+00:00

Hi,

I have this code in windows , net 9.

I'm download a big file file (5 giga) from our website when using DownloadCommand

and action is failed but I got success message.

This is the problematic start code , how to fix it ?

Thanks,

}).ContinueWith(async task1 =>

{

var results = await task1.Result;// null

var contentStream = results.Item1;

// no data return

if (contentStream == null || string.IsNullOrEmpty(results.Item2)) //here it continue to other continuations event if it failed look on attached code.

{

_logger.Error($"No data return from download for identiffiers:{sbFilesIdentifiers}");

return;

}

Developer technologies | .NET | .NET MAUI
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 78,316 Reputation points Volunteer Moderator
    2025-03-27T23:40:03.0666667+00:00

    reading a large download is two steps

    • read the headers. this is where the status code 200 (OK) read
    • stream the body. in a large download this can easily fail due to any network error.

    you have rather convoluted code. you seem to create a task for no reason, return a new task with the stream. convert an async call into sync, why not simple:

    var zipPathForFolderOrPathForFile = string.Empty;
    var container = new Bootstrapper().Bootstrap();
    Constants.RequestBaseAddress = LoginConfigurations.GetInstance().Portal;
    IAutomationApiClient automationApiClient = container.Resolve<IAutomationApiClient>();
    
    // start fetch
    var (contentStream, fileNameResults) = await automationApiClient.PostDownloadFiles($"{LoginConfigurations.GetInstance().Portal}/api/HandleFiles/PostDownloadFiles", SelectedPathItem.Value, sbFilesIdentifiers.ToString(), true, LoginConfigurations.GetInstance().SessionToken);
    if (contentStream == null || string.IsNullOrEmpty(fileNameResults))
    {
        _logger.Error($"No data return from download for identiffiers:{sbFilesIdentifiers}");
        return;
    }
    
    // zip file path
    zipPathForFolderOrPathForFile = Path.Combine(
        SelectedItem.Path == null 
            ? SelectedItem.Name.TrimEnd('\\').TrimEnd('\\') 
            : SelectedItem.Path, fileNameResults
    );
    
    // Create the file stream in destination
    var fileStream = File.Create(zipPathForFolderOrPathForFile);
    
    // fetch file data from server and copy to filestream
    await contentStream.CopyToAsync(fileStream);
    
    // these should be in a finally block
    fileStream.Dispose();
    contentStream.Dispose();
    
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.