2

I haven't had luck uploading more than 4MB to a OneDrive, I have successfully Created Folder, rename, delete, and even upload a file that is 4MB or less, however uploading something more than 4MB seems to be a little more complicated. I have been trying to understand by looking at this solution How to upload a large document in c# using the Microsoft Graph API rest calls

There are two solutions. The solution with higher votes suggests a lengthy method (however the function "GetChunkRequestResponseAsync" is deprecated, and I haven't been able to find a suitable function to perform the same), and the second solution uses "new LargeFileUpload", however when I put this in my code (Visual Studio), it tells me that only "LargeFileUploadTask < ? > " exists (same function by appearance however has a "Task" at the end. I don't understand what to put "Task < string > ???".

Anyways, I definitely understand that an uploadSession must be Requested:

var uploadSession = await _client.Drive.Items[FolderID]
                    .ItemWithPath(Name)
                    .CreateUploadSession()
                    .Request()
                    .PostAsync();
var maxChunkSize = 320 * 1024; //320 KB chunk sizes 

and it may involve storing data into a byte array such like:

string FilePath = "D:\\MoreThan5MB.txt";
string path = FilePath;//Actual File Location in your hard drive       

byte[] data = System.IO.File.ReadAllBytes(path);  //Stores all data into byte array by name of "data" then "PUT to the root folder

Stream stream = new MemoryStream(data);

But any help and suggestions would be greatly appreciated. If it means anything, here is a link of someone helping me upload less than 4MB: How to upload to OneDrive using Microsoft Graph Api in c# Thanks

1 Answer 1

3

Microsoft has made the upload of large files significantly easier, since the posts to the question. I am currently using MS Graph 1.21.0. This code should work with some small adjustments:

    public async Task<DriveItem> UploadToFolder(
        string driveId,
        string folderId,
        string fileLocation,
        string fileName)
    {
        DriveItem resultDriveItem = null;

        using (Stream fileStream = new FileStream(
                    fileLocation,
                    FileMode.Open,
                    FileAccess.Read))
        {
            var uploadSession = await _graphServiceClient.Drives[driveId].Items[folderId]
                                    .ItemWithPath(fileName).CreateUploadSession().Request().PostAsync();

            int maxSlice = 320 * 1024;

            var largeFileUpload = new LargeFileUploadTask<DriveItem>(uploadSession, fileStream, maxSlice);

            IProgress<long> progress = new Progress<long>(x =>
            {
                _logger.LogDebug($"Uploading large file: {x} bytes of {fileStream.Length} bytes already uploaded.");
            });

            UploadResult<DriveItem> uploadResult = await largeFileUpload.UploadAsync(progress);

            resultDriveItem = uploadResult.ItemResponse;
        }

        return resultDriveItem;
    }
Sign up to request clarification or add additional context in comments.

1 Comment

This was very helpful, and has solved my problem. What I noticed was that in my 'less than 4MB upload' script, it uses "Stream stream = new MemoryStream(byte [ ])", and here I said "Stream stream = new FileStream( byte [ ] )", so basically it needed to be Filestream. Then use: "var largeFileUpload = new LargeFile....." (entire line), and "UploadResult<DriveItem> upload Result = await...." (entire line) I'm mainly highlighting which lines were important for future reference.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.