How to upload small files on onedrive with IFormFile and Batch request of Microsoft graph api

NIAMA Megane 0 Reputation points
2023-07-13T09:19:40.0866667+00:00

Hi,

i would like to know if it's actually possible to upload small files with microsoft graph api to an onedrive using Batch Request. My files do upload but unfortunately, when i try to open them they are blank, i guess my error come from how i put the content.

Here what i have so far.

Thanks in advance for any advice or help you can provide

     public async Task BatchUploadFileAsync(GetAllFilesToUpload filesToUpload)

        {

            int batchSize = 20;

            var batchesOfFiles = new List<List<FileDatatoUpload>>();

            var currentBatch = new List<FileDatatoUpload>();

            foreach (var file in filesToUpload.Files)

            {

                currentBatch.Add(file);

                if (currentBatch.Count == batchSize)

                {

                    batchesOfFiles.Add(currentBatch);

                    currentBatch = new List<FileDatatoUpload>();

                }

            }

            if (currentBatch.Count > 0)

            {

                batchesOfFiles.Add(currentBatch);

                currentBatch = new List<FileDatatoUpload>();

            }

            try

            {

                List<System.IO.Stream> streams = new List<System.IO.Stream>();

                foreach (var batchFiles in batchesOfFiles)

                {

                    var batchRequestContent = new BatchRequestContent();

                    foreach (var file in batchFiles)

                    {

                        // Créer et ouvrir le stream

                        var fileStream = file.FormFile.OpenReadStream();

                        // Ajouter le stream à la liste

                        streams.Add(fileStream);

                        Console.WriteLine($"Uploading file {file.FormFile.FileName} to {file.PathStudent}");

                        //using (var fileStream = file.FormFile.OpenReadStream())

                        //{

                        var jsonObject = new

                        {

                            contentBytes = new StreamContent(fileStream)

                        };

                        // Sérialisation en JSON

                        string jsonContent = JsonConvert.SerializeObject(jsonObject);

                        var stringContent = new StringContent(jsonContent, Encoding.UTF8, "application/json");

                        var request = _graphServiceClientApp.Users["userID"].Drive.Root

                                .ItemWithPath(file.PathStudent)

                                .Content.Request().GetHttpRequestMessage();

                            request.Method = HttpMethod.Put;

                            request.Content = stringContent ;

                        Console.WriteLine($"Request URI: {request.Content}");

                        // Add upload request to batch

                        batchRequestContent.AddBatchRequestStep(request);

                        //}

                    }

                    // Send the batch request

                    var response = await _graphServiceClientApp.Batch.Request().PostAsync(batchRequestContent);

                }

                foreach (var stream in streams)

                {

                    stream.Close();

                }

            }

            catch (ServiceException ex)

            {

                Console.WriteLine($"Error uploading: {ex.ToString()}");

            }

        }
Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
13,059 questions
OneDrive
OneDrive
A Microsoft file hosting and synchronization service.
1,314 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.