Qiuzman
Qiuzman

Reputation: 1761

Using Box SDK to get a thumbnail representation of a file

I have created the following code to get base64 string of a file to return in my API however the string it returns does not seem to be useable base64 as it fails in every base64 viewer:

        public async Task<string> GetThumbnailImageById(string Id)
    {
        BoxClient adminClient = _session.AdminClient(await token.FetchTokenAsync());

        var requestParams = new BoxRepresentationRequest()
        {
            FileId = Id,
            XRepHints = "[png?dimensions=94x94]"
        };

       
        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())
                {
                    (await httpClient.GetStreamAsync(url)).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 am unsure what I am doing wrong in this procedure to create unusable base64. Any help would be appreciated.

Update: Upon further research it seems the stream seems to contain errors in it such as the length not being supported and such so I am thinking something else is failing. The box api for accessing this resource is here. Any help would be appreciated.

Upvotes: 0

Views: 18

Answers (0)

Related Questions