在OneDrive中上载文件时,对象引用未设置为对象

时间:2017-06-19 07:57:31

标签: microsoft-graph onedrive

我正在使用Microsoft Graph SDK在OneDrive中以块的形式上传文件。我使用下面的代码上传文件:

try
{
    GraphServiceClient graphClient = this.GetGraphServiceClient(accessToken);

    string fileName = Path.GetFileName(srcFilePath);

    using (var fileContentStream = System.IO.File.Open(srcFilePath, System.IO.FileMode.Open))
    {
        var uploadSession = await graphClient.Me.Drive.Root.ItemWithPath(fileName).CreateUploadSession().Request().PostAsync();

        var maxChunkSize = 5 * 1024 * 1024;
        var provider = new ChunkedUploadProvider(uploadSession, graphClient, fileContentStream, maxChunkSize);

        var chunkRequests = provider.GetUploadChunkRequests();
        var readBuffer = new byte[maxChunkSize];
        var trackedExceptions = new List<Exception>();
        Microsoft.Graph.DriveItem itemResult = null;

        foreach (var request in chunkRequests)
        {
            var result = await provider.GetChunkRequestResponseAsync(request, readBuffer, trackedExceptions);

            if (result.UploadSucceeded)
            {
                itemResult = result.ItemResponse;
            }
        }
    }
}
catch (Microsoft.Graph.ServiceException e)
{
}
catch (Exception ex)
{ 
}

以上代码适用于普通文件名。但是,当我尝试上传名称为 Test#123.pdf 的文件时,&#34;对象引用未设置为对象&#34;行var provider = new ChunkedUploadProvider(uploadSession, graphClient, fileContentStream, maxChunkSize);引发异常请参见下面的屏幕截图:enter image description here

这是OneDrive SDK的限制,还是我没有正确传递参数?

1 个答案:

答案 0 :(得分:4)

#符号在URL中具有特殊含义。在您可以使用之前,您需要对文件名进行编码:Test%23123.pdf