Freigeben über


Hochladen großer Dateien mithilfe der Microsoft Graph SDKs

Eine Reihe von Entitäten in Microsoft Graph unterstützt fortsetzbare Dateiuploads , um das Hochladen großer Dateien zu vereinfachen. Anstatt zu versuchen, die gesamte Datei in einer einzelnen Anforderung hochzuladen, wird die Datei in kleinere Teile aufgeteilt, und eine Anforderung wird verwendet, um einen einzelnen Slice hochzuladen. Um diesen Prozess zu vereinfachen, implementieren die Microsoft Graph SDKs eine Aufgabe zum Hochladen großer Dateien, die das Hochladen der Slices verwaltet.

Hochladen einer großen Datei auf OneDrive

using var fileStream = File.OpenRead(filePath);

// Use properties to specify the conflict behavior
// using DriveUpload = Microsoft.Graph.Drives.Item.Items.Item.CreateUploadSession;
var uploadSessionRequestBody = new DriveUpload.CreateUploadSessionPostRequestBody
{
    Item = new DriveItemUploadableProperties
    {
        AdditionalData = new Dictionary<string, object>
        {
            { "@microsoft.graph.conflictBehavior", "replace" },
        },
    },
};

// Create the upload session
// itemPath does not need to be a path to an existing item
var myDrive = await graphClient.Me.Drive.GetAsync();
var uploadSession = await graphClient.Drives[myDrive?.Id]
    .Items["root"]
    .ItemWithPath(itemPath)
    .CreateUploadSession
    .PostAsync(uploadSessionRequestBody);

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

var totalLength = fileStream.Length;
// Create a callback that is invoked after each slice is uploaded
IProgress<long> progress = new Progress<long>(prog =>
{
    Console.WriteLine($"Uploaded {prog} bytes of {totalLength} bytes");
});

try
{
    // Upload the file
    var uploadResult = await fileUploadTask.UploadAsync(progress);

    Console.WriteLine(uploadResult.UploadSucceeded ?
        $"Upload complete, item ID: {uploadResult.ItemResponse.Id}" :
        "Upload failed");
}
catch (ODataError ex)
{
    Console.WriteLine($"Error uploading: {ex.Error?.Message}");
}

Fortsetzen eines Dateiuploads

Die Microsoft Graph SDKs unterstützen das Fortsetzen laufender Uploads. Wenn ihre Anwendung während des Uploads eine Verbindungsunterbrechung oder eine 5.x.x-HTTP-status auftritt, können Sie den Upload fortsetzen.

await fileUploadTask.ResumeAsync(progress);

Hochladen einer großen Anlage in Outlook-Nachricht

// Create message
var draftMessage = new Message
{
    Subject = "Large attachment",
};

var savedDraft = await graphClient.Me
    .Messages
    .PostAsync(draftMessage);

using var fileStream = File.OpenRead(filePath);
var largeAttachment = new AttachmentItem
{
    AttachmentType = AttachmentType.File,
    Name = Path.GetFileName(filePath),
    Size = fileStream.Length,
};

// using AttachmentUpload = Microsoft.Graph.Me.Messages.Item.Attachments.CreateUploadSession;
var uploadSessionRequestBody = new AttachmentUpload.CreateUploadSessionPostRequestBody
{
    AttachmentItem = largeAttachment,
};

var uploadSession = await graphClient.Me
    .Messages[savedDraft?.Id]
    .Attachments
    .CreateUploadSession
    .PostAsync(uploadSessionRequestBody);

// Max slice size must be a multiple of 320 KiB
int maxSliceSize = 320 * 1024;
var fileUploadTask =
    new LargeFileUploadTask<FileAttachment>(uploadSession, fileStream, maxSliceSize);

var totalLength = fileStream.Length;
// Create a callback that is invoked after each slice is uploaded
IProgress<long> progress = new Progress<long>(prog =>
{
    Console.WriteLine($"Uploaded {prog} bytes of {totalLength} bytes");
});

try
{
    // Upload the file
    var uploadResult = await fileUploadTask.UploadAsync(progress);
    Console.WriteLine(uploadResult.UploadSucceeded ? "Upload complete" : "Upload failed");
}
catch (ODataError ex)
{
    Console.WriteLine($"Error uploading: {ex.Error?.Message}");
}