"maximum request length exceeded" 将文件上传到 Onedrive 时
"maximum request length exceeded" When uploading a file to Onedrive
我使用 "OneDriveApiBrowser" 中的示例代码作为向我的应用程序添加保存到一个驱动器支持的基础。这利用了 Microsoft.Graph,我可以上传小文件,但较大的文件 (10Mb) 不会上传并给出错误 "maximum request length exceeded"。我的应用程序和示例代码都出现了同样的错误,代码行如下:
DriveItem uploadedItem = await graphClient.Drive.Root.ItemWithPath(drivePath).Content.Request().PutAsync<DriveItem>(newStream);
有没有办法增加可上传文件的最大大小?如果有怎么办?
Graph 仅接受使用 PUT-to-content 的小文件,因此您需要查看 creating an upload session. Since you're using the Graph SDK I'd use this test case as a guide。
这里有一些完整的代码 - 它不会直接编译,但它应该让您看到所涉及的步骤:
var uploadSession = await graphClient.Drive.Root.ItemWithPath("filename.txt").CreateUploadSession().Request().PostAsync();
var maxChunkSize = 320 * 1024; // 320 KB - Change this to your chunk size. 5MB is the default.
var provider = new ChunkedUploadProvider(uploadSession, graphClient, inputStream, maxChunkSize);
// Setup the chunk request necessities
var chunkRequests = provider.GetUploadChunkRequests();
var readBuffer = new byte[maxChunkSize];
var trackedExceptions = new List<Exception>();
DriveItem itemResult = null;
//upload the chunks
foreach (var request in chunkRequests)
{
var result = await provider.GetChunkRequestResponseAsync(request, readBuffer, trackedExceptions);
if (result.UploadSucceeded)
{
itemResult = result.ItemResponse;
}
}
我使用 "OneDriveApiBrowser" 中的示例代码作为向我的应用程序添加保存到一个驱动器支持的基础。这利用了 Microsoft.Graph,我可以上传小文件,但较大的文件 (10Mb) 不会上传并给出错误 "maximum request length exceeded"。我的应用程序和示例代码都出现了同样的错误,代码行如下:
DriveItem uploadedItem = await graphClient.Drive.Root.ItemWithPath(drivePath).Content.Request().PutAsync<DriveItem>(newStream);
有没有办法增加可上传文件的最大大小?如果有怎么办?
Graph 仅接受使用 PUT-to-content 的小文件,因此您需要查看 creating an upload session. Since you're using the Graph SDK I'd use this test case as a guide。
这里有一些完整的代码 - 它不会直接编译,但它应该让您看到所涉及的步骤:
var uploadSession = await graphClient.Drive.Root.ItemWithPath("filename.txt").CreateUploadSession().Request().PostAsync();
var maxChunkSize = 320 * 1024; // 320 KB - Change this to your chunk size. 5MB is the default.
var provider = new ChunkedUploadProvider(uploadSession, graphClient, inputStream, maxChunkSize);
// Setup the chunk request necessities
var chunkRequests = provider.GetUploadChunkRequests();
var readBuffer = new byte[maxChunkSize];
var trackedExceptions = new List<Exception>();
DriveItem itemResult = null;
//upload the chunks
foreach (var request in chunkRequests)
{
var result = await provider.GetChunkRequestResponseAsync(request, readBuffer, trackedExceptions);
if (result.UploadSucceeded)
{
itemResult = result.ItemResponse;
}
}