0

I have a C# code that reads a Microsoft document file (.docx), and uploads it to Microsoft SharePoint (Office 365). The way that I read the file and stream is like the code snippet below:

using var stream = File.OpenRead("MyDocument.docx");
//And I upload the "stream" to Office 365 using Microsoft Graph API

The uploaded file to office 365 can successfully be opened in Microsoft Word. I wrapped the very same code piece that uploads files to Microsoft SharePoint in an Azure HTTP-triggered function and had POSTMAN submit the same file. The function looks like the following pattern:

    [Function(nameof(DocumentUploader))]
    public Task<HttpResponseData?> UploadAsync(
    [HttpTrigger(AuthorizationLevel.Function, "post", Route = $"{nameof(DocumentUploader)}/{{filename}}/{{extension}}")]
        HttpRequestData request,
        string filename,
        string extension)
{
var stream = request.Body;
//The rest of the code to upload the stream to office 365 is the same
}

I realized that the length of stream or request.Body is greater than what File.OpenRead(...) yields. The consequence is that the uploaded stream to office 365 gets corrupted and it fails to open.

Any idea how to fix this problem to be able to upload the right content?

enter image description here

Arash
  • 3,628
  • 5
  • 46
  • 70
  • Can you show us the request from Postman? – phuzi Sep 20 '22 at 14:42
  • @phuzi Done, please see the updated question that now contains the screenshot. – Arash Sep 20 '22 at 15:10
  • _"I have a C# code that reads a Microsoft document file (.docx), and uploads it to Microsoft SharePoint (Office 365)"_ Could you show us the relevant bits of this code too? – phuzi Sep 20 '22 at 15:43
  • @phuzi Please see the answer below. Fortunately POSTMAN does not exhibit any problem. Thanks for the help though. – Arash Sep 20 '22 at 16:03

1 Answers1

0

The solution is per this Stackoverflow post. We need to use a 3rd party library to parse the request's body.

Arash
  • 3,628
  • 5
  • 46
  • 70