Problems downloading file using SDK
Each time when I try to download the file using the code below causes the problem. It never downloads the complete file. If a file is 1 MB then it will download 100 KB then next time its downloads 188 KB. Each time I run the process I get the different size of the file. Plus If I want to move the file to another folder then it gives an error that File is in use.
Here is the code I got from User recipes.
class Program
{
static void Main(string[] args)
{
ExecuteMainAsync().Wait();
}
private static async Task ExecuteMainAsync()
{
using (FileStream fs = new FileStream($"./config.json", FileMode.Open))
{
var session = new BoxJWTAuth(BoxConfig.CreateFromJsonFile(fs));
var client = session.AdminClient(session.AdminToken());
var folderId = "987654321";
var folder = await client.FoldersManager.GetInformationAsync(folderId);
var folderName = folder.Name;
var localFolderPath = Path.Combine(Directory.GetCurrentDirectory(), folderName);
ResetLocalFolder(localFolderPath);
var items = await client.FoldersManager.GetFolderItemsAsync(folderId, 1000, autoPaginate: true);
var fileDownloadTasks = new List<Task>();
var files = items.Entries.Where(i => i.Type == "file");
foreach (var file in files)
{
fileDownloadTasks.Add(client.FilesManager.DownloadStreamAsync(file.Id).ContinueWith((t) =>
{
var localFile = File.Create(Path.Combine(localFolderPath, file.Name));
return t.Result.CopyToAsync(localFile);
}));
}
await Task.WhenAll(fileDownloadTasks);
}
}
private static void ResetLocalFolder(string localFolderPath)
{
if (!Directory.Exists(localFolderPath))
{
Directory.CreateDirectory(localFolderPath);
}
else
{
foreach (var file in Directory.EnumerateFiles(localFolderPath))
{
File.Delete(Path.Combine(localFolderPath, file));
}
Directory.Delete(localFolderPath);
Directory.CreateDirectory(localFolderPath);
}
}
}
}
.
-
Do you ever receive an error message? If so, could you please provide:
- full body response of the error
- date/time/timezone you receive the error
- client ID of your application
Alternatively, please open a support ticket at support.box.com with all relevant details. Unfortunately code review is outside of the support scope, but we'd be happy to help further investigate!
Best,
Kourtney
Box Technical Support Engineer
Please sign in to leave a comment.
Comments
1 comment