繁体   English   中英

无法使用 Microsoft Graph SDK 上传文件

[英]Unable to upload file using Microsoft Graph SDK

我正在尝试使用 Microsoft 的 Graph SDK 上传文件,但遇到了问题。

我已经从这里逐字复制了 C# 示例,注释掉了进度部分并更新了 C# 8 的using语句,这就是我所拥有的......

public async Task<bool> UploadFileAsync(string parentFolderId, string filename, byte[] bytes)
{
    var graphClient = GetGraphClient();

    // Declare the variable outside the `using` statement to get around a little C# problem - https://www.tabsoverspaces.com/233779-using-await-using-iasyncdisposable-with-configureawait
    var memoryStream = new MemoryStream(bytes);
    await using (memoryStream.ConfigureAwait(false))
    {
        // Use properties to specify the conflict behavior.
        // - in this case, replace.
        var uploadProps = new DriveItemUploadableProperties
                          {
                              AdditionalData = new Dictionary<string, object> {{"@microsoft.graph.conflictBehavior", "replace"}},
                              ODataType = null
                          };
        try
        {
            // Create the upload session.
            // - itemPath does not need to be a path to an existing item.
            var uploadSession = await graphClient.Drives[_driveId]
                                                 .Items[parentFolderId]
                                                 .ItemWithPath(filename)
                                                 .CreateUploadSession(uploadProps)
                                                 .Request()
                                                 .PostAsync()
                                                 .ConfigureAwait(false);

            // Max slice size must be a multiple of 320KB.
            const int maxSliceSize = 320 * 1024;
            var fileUploadTask = new LargeFileUploadTask<DriveItem>(uploadSession, memoryStream, maxSliceSize);

            // Upload the file.
            var uploadResult = await fileUploadTask.UploadAsync().ConfigureAwait(false);

            if (uploadResult.UploadSucceeded)
            {
                // The ItemResponse object in the result represents the created item.
                return true;
            }

            return false;
        }
        catch (ServiceException exception)
        {
            // ...
        }
    }
}

然而线...

var uploadSession = await graphClient.Drives[_driveId]
                                     .Items[parentFolderId]
                                     ...

...引发异常:

Microsoft.Graph.ServiceException:代码:BadRequest 消息:使用“microsoft.graph.createUploadSession”的相同绑定参数发现了多个操作重载。

任何人都可以帮忙吗?

我找出了问题的原因 - 我使用的文件名包含无效符号(在我的情况下,我正在对 DateTime 进行字符串化并且包含:

令人沮丧的是,这个异常没有正确冒泡,而是我收到了“多动作重载”消息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM