box / box-windows-sdk-v2

Windows SDK for v2 of the Box API. The SDK is built upon .NET Framework 4.5

Home Page:https://developer.box.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Net frame 4.8/ Issue with file download (Async) feature

arangaramu opened this issue · comments

I had to change my project from .net framework 4.5 to .net frame 4.8 to get the support of some libraries.
I didnt had any issues to compile.

However now i see that box fails to download multiple files in the new configuration. I dont have any issues to autheticate but multiple files fail in the newer configuratin.

I tried both folder sync or individual file sync. The download is very random. It doenst complete but it starts.

                //Folder contents at once
                fileDownloadTasks.Add(userClient.FilesManager.DownloadAsync(item.Id).ContinueWith((t) =>
                {
                    return t.Result.CopyToAsync(File.Create(localFile));
                }));

                // One file at a time
                Stream fileContents = await userClient.FilesManager.DownloadAsync(item.Id);
                if (fileContents != null)
                {
                    fileContents.CopyTo(File.Create(localFile));
                }

Does anyone know why this is and how to fix it.

I did refer to 8 year old post and follow the instructions. Now with the below example it works as expected.

https://stackoverflow.com/questions/23575204/using-box-v2-api-a-downloadstreamasync-call-is-resulting-in-corrupt-file

Could API team include this fix in their Async feature and fix the problem.

Hello @arangaramu, thank for let us know about that.

I've created .net project targeting .net framework 4.8 and put there your sample code:

  // One file at a time
  Stream fileContents = await userClient.FilesManager.DownloadAsync(item.Id);
  if (fileContents != null)
  {
      fileContents.CopyTo(File.Create(localFile));
  }

This works for me for every file I tired to download.

So I have a few questions for you and I will be grateful for your answer:

  1. Is this always fails on your local environment? Are these files specific?
  2. Has the same code worked before on .net framework 4.5?
  3. Can you create a sample console app targeting .net framework 4.8 and try to download file there?

@arangaramu yes, if you could send me this sample app, that would be great.

@arangaramu one more thing came to my mind. Can you try to add this instead of previous snippet?
I think that this issue could be because of not closing Stream.

using (Stream fileContents = await this.client.FilesManager.DownloadAsync(item.Id))
{
    using (FileStream output = File.Create(localFile))
    {
        if (fileContents != null)
        {
            fileContents.CopyTo(output);
        }
    }
}