Yesterday I blogged about uploading files through Postman. Let's now move on to the real application and implement the client API to upload the files using the .NET Core HttpClient.
The code is not that hard but I'm sharing it here just a reminder for myself:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var sourceFile ="ExampleDocument.docx"; | |
using var form = new MultipartFormDataContent(); | |
using var fileContent = new ByteArrayContent(await File.ReadAllBytesAsync(sourceFile)); | |
fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/form-data"); | |
form.Add(fileContent, "files", fileName:sourceFile); | |
var response = await Client.PostAsync("/convert/office", form); | |
response.EnsureSuccessStatusCode(); |