1

使用 Microsoft Graph C# SDK v1.13。我能够IDriveItemThumbnailsCollectionPage为我的DriveItem. 有了那个页面,我可以得到第一个ThumbnailSet. 那ThumbnailSet有一个小、中、大的缩略图对象。

每个缩略图对象似乎都有一个有效的缩略图 URL。但是,没有一个.Content(Stream) 属性是有效的......它们都是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 调试器查看返回的变量,“thumbs”填充了大小和 URL,但尝试检索内容始终为 NULL。

4

1 回答 1

1

由于Thumbnail.Url属性用于存储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

另一种选择是使用以下端点(来自 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();
于 2019-01-22T14:20:24.250 回答