I have been struggling to get a thumbnail representation through the .net sdk. I am able to get the url for the download but everytime I try to download it through an httpclient request to return a stream, the stream results and memorystream converted to base64 are unusable. I am unsure if my approach is not correct or if there is an issue with the sdk. My code is as follows:
public async Task<string> GetThumbnailImageById(string Id)
{
BoxClient adminClient = _session.AdminClient(await token.FetchTokenAsync());
var requestParams = new BoxRepresentationRequest()
{
FileId = Id,
XRepHints = "[png?dimensions=1024x1024]"
};
BoxRepresentationCollection<BoxRepresentation> representations = await adminClient.FilesManager
.GetRepresentationsAsync(requestParams);
var url = representations.Entries.FirstOrDefault().Info.Url.ToString();
try{
using (var httpClient = _httpClientFactory.CreateClient())
{
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", await token.FetchTokenAsync());
using (var memoryStream = new MemoryStream())
{
var stream = await httpClient.GetStreamAsync(url);
(stream).CopyTo(memoryStream);
memoryStream.Position = 0;
byte[] buffer = new byte[memoryStream.Length];
await memoryStream.ReadAsync(buffer, 0, (int)memoryStream.Length);
return Convert.ToBase64String(buffer);
}
}
} catch (Exception ex)
{
return "";
}
}