缩略图对象具有网址,但没有内容

时间:2019-01-21 21:58:39

标签: c# microsoft-graph microsoft-graph-sdks

使用Microsoft Graph C#SDK v1.13。我可以为我的IDriveItemThumbnailsCollectionPage检索一个DriveItem。通过该页面,我可以获得第一个ThumbnailSet。该ThumbnailSet有一个小型,中型和大型Thumbnail对象。

这些缩略图对象中的每一个似乎都有一个有效的缩略图URL。但是,.Content(流)属性都不有效……它们都是NULL

例如:

IDriveItemThumbnailsCollectionPage page = await client
    .Me
    .Drive
    .Items[this.Id]
    .Thumbnails
    .Request()
    .GetAsync();

if ((page != null) && (page.CurrentPage != null) && (page.CurrentPage.Count > 0)) {
    ThumbnailSet thumbs = page.CurrentPage.FirstOrDefault();
    if (thumbs != null) {
        if ((minHeight != null) && (minHeight.HasValue) && (minWidth != null) && (minWidth.HasValue)) {
            if ((minHeight > 48) || (minWidth > 48)) {
                Thumbnail thumb = thumbs.Medium;
                thumbStream = thumb?.Content;
            }
        }

        if (thumbStream == null)
            thumbStream = thumbs?.Small?.Content;
    }
}

如何通过SDK获得这些Thumbnail的内容流之一?

使用VS调试器查看返回的var,“ thumb”填充了大小和URL,但是尝试检索Content始终为NULL。

1 个答案:

答案 0 :(得分:1)

由于Thumbnail.Url property用于存储获取缩略图内容的URL ,因此下载缩略图内容的最简单的选择可能是这样的:

var page = await graphClient.Me.Drive.Items[itemId].Thumbnails.Request().GetAsync();
var thumbnailSet = page.FirstOrDefault();
var thumbnail = thumbnailSet?.Medium;
using (var client = new System.Net.WebClient())
{
    var content = client.DownloadData(thumbnail.Url);  //download it as a byte array

    System.IO.File.WriteAllBytes(targetFileName, content); //save into file
}

选项2

另一种选择是利用以下端点(from docs):

GET /me/drive/items/{item-id}/thumbnails/{thumb-id}/{size}/content

以下是msgraph-sdk-dotnet的示例:

//1.get thumbnail metadata (similar to previous example)
var requestUrl = $"{graphClient.BaseUrl}/me/drive/items/{itemId}/thumbnails/{thumbnailId}/{thumbnailSize}";
var message = new HttpRequestMessage(HttpMethod.Get, requestUrl);
await graphClient.AuthenticationProvider.AuthenticateRequestAsync(message);
var response = await graphClient.HttpProvider.SendAsync(message);
var content = await response.Content.ReadAsStringAsync();
var thumbnail = JsonConvert.DeserializeObject<Thumbnail>(content);

//2.get thumbnail content
requestUrl = $"{graphClient.BaseUrl}/me/drive/items/{itemId}/thumbnails/{thumbnailId}/{thumbnailSize}/content";
message = new HttpRequestMessage(HttpMethod.Get, requestUrl);
await graphClient.AuthenticationProvider.AuthenticateRequestAsync(message);
response = await graphClient.HttpProvider.SendAsync(message);
var bytesContent = await response.Content.ReadAsByteArrayAsync();