简体   繁体   中英

Unable to upload file using Microsoft Graph SDK

I'm trying to upload a file using Microsoft's Graph SDK but have hit a problem.

I have pretty much copied verbatim the C# example from here , commented-out the progress part and updated the using statement for C# 8, and here's what I have...

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)
        {
            // ...
        }
    }
}

However the line...

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

...throws an exception:

Microsoft.Graph.ServiceException: Code: BadRequest Message: Multiple action overloads were found with the same binding parameter for 'microsoft.graph.createUploadSession'.

Can anyone help?

I figured out the cause of the problem - the filename that I was using contained invalid symbols (in my case I was stringifying a DateTime and that contained : ).

It's frustrating that this exception doesn't bubble-up correctly and instead I got that "Multiple action overloads" message.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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