I have written a Web API function to upload a file and with some other properties, like UserId, Title, and Version. The purpose of this API is to upload a file to the Azure Blob and save the other data to the local database.
Here is the model which the API gets:
public class DocumentModel
{
[JsonPropertyName("file")]
public IFormFile File { get; set; }
[JsonPropertyName("userId")]
[Required]
public string UserId { get; set; }
[JsonPropertyName("title")]
[Required]
public string Title { get; set; }
[JsonPropertyName("version")]
[Required]
public string Version { get; set; }
}
And here is the API function:
[HttpPost]
[Route("addDocument")]
public async Task<IActionResult> CreateDocument([FromForm] DocumentModel model)
{
if (!ModelState.IsValid)
{
return BadRequest("Invalid document create request.");
}
var connectionString = _settings.appConfig.StorageConnectionString;
BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
var blobContainer = blobServiceClient.GetBlobContainerClient(model.UserId);
blobContainer.CreateIfNotExists();
BlobClient blobClient = blobContainer.GetBlobClient(model.DocumentPath);
using (var ms = new MemoryStream())
{
model.File.CopyTo(ms);
var fileBytes = ms.ToArray();
BinaryData uploadData = new BinaryData(fileBytes);
await blobClient.UploadAsync(uploadData, true);
}
return Ok(model);
}
Now I have to write a Console Application in C# to consume that "file upload API" and pass the model object to that API. Now I don't know how to prepare the model as it has the "File" property which is IFormFile type.
Can anyone suggest to me how to prepare the above model and pass it to the API with HttpClient class or RestShar library in .net?
I am using .net Core 6