Skip to main content

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 "";

}

}

I have successfully been able to pull thumbnails using the code below but a representation is not always available unless i run it multiple times. Are representations not generated on each request for the supported file types? I also notice even though some representations say a xlsb file type is supported there is never any representation available. So I am confused as to why this seems inconsistent? 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 = "[jpg?dimensions=320x320]"

};





BoxRepresentationCollection<BoxRepresentation> representations = await adminClient.FilesManager

.GetRepresentationsAsync(requestParams);



var representation = representations.Entries.FirstOrDefault();

if (representation is not null)

{

try{

using (var httpClient = _httpClientFactory.CreateClient())

{

httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", await token.FetchTokenAsync());



using (var memoryStream = new MemoryStream())

{

Stream stream;

if (representation.Properties.Paged)

{

stream = await httpClient.GetStreamAsync(representation.Content.UrlTemplate.Replace("{+asset_path}", "1.jpg"));

}

else

{

stream = await httpClient.GetStreamAsync(representation.Content.UrlTemplate.Replace("{+asset_path}", ""));

}



stream.CopyTo(memoryStream);

memoryStream.Position = 0;



byte[] buffer = new byte[memoryStream.Length];

await memoryStream.ReadAsync(buffer, 0, (int)memoryStream.Length);

return "data:image/png;base64," + Convert.ToBase64String(buffer);

}

}

} catch (Exception ex)

{

return "not found";

}

}



return "not found";

}


Hi @shaun,



According to the documentation, the logic for downloading thumbnails should be based on the representation.Status.State field, as detailed here.







  • When the field has the value success, we can retrieve the content from the content.url_template field.







  • When the field has the value pending, we must wait until the thumbnail is generated, periodically checking its status.







  • When the field has the value none, then to trigger the thumbnail generation we need to call the Request method with the URL from the info.url field. After that, we check the status again.







In summary, you should only retrieve the file representation when its status is success.



As for .xlsb support, as I can see on this page, this format is unfortunately not supported.



Hope this helps!


Actually, to download the representation you’re interested in, you can call the GetRepresentationContentAsync method, which will handle everything I described above for you 🙂


I actually did come across this method and it is cleaner. Is there any file type or my thumbnails which would probably be quicker than others (ie, png, jpg 32 x 32, etc)? I notice some files even pdf and such take forever for this function to return a response. Some do not even return a response such as xlsb files (error “Could not get requested representation!”) despite box says its supported for representations as thumbnails. My updated 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 = " jpg?dimensions=320x320]"

};



try

{

using (var memoryStream = new MemoryStream())

{

Stream stream;

stream = await adminClient.FilesManager.GetRepresentationContentAsync(requestParams);



stream.CopyTo(memoryStream);

memoryStream.Position = 0;



byteb] buffer = new bytebmemoryStream.Length];

await memoryStream.ReadAsync(buffer, 0, (int)memoryStream.Length);

return "data:image/jpg;base64," + Convert.ToBase64String(buffer);

}

}

catch (Exception ex)

{

return "not found";

}



}


Hi @shaun ,



Regarding the supported document types, they are listed here: Box Developer - Thumbnail Representation, where you can see that unfortunately, .xlsb is not supported.



As for the functionality of the thumbnail retrieval and its duration, I don’t have knowledge about this because it falls outside the domain of the SDK and pertains to the system. If this is a problem, I suggest that you start a new topic directly inquiring about the API’s operation, and you will surely receive a response.



Best regards,


Artur


Reply