0

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

mnu-nasir
  • 1,642
  • 5
  • 30
  • 62
  • https://stackoverflow.com/questions/55412899/how-to-post-form-data-iformfile-with-httpclient have you tried this? – Lee Jun 29 '22 at 07:41

2 Answers2

1

I have several examples, maybe it will give you an idea how it worsks https://github.com/managed-code-hub/Storage/blob/main/Samples/WebApiSample/Controllers/StorageController.cs

try passing the IFromFile in the body of the request and for parameters use query string. and in console app: C# HttpClient 4.5 multipart/form-data upload

ksemenenko
  • 11
  • 1
1

Try this. it works for me.

public static async Task Main()
{
    var _httpClient = new HttpClient();

    var formContent = new MultipartFormDataContent();

    var FilePath = @"D:\test.txt";

    formContent.Add(new StreamContent(File.OpenRead(FilePath)), "File", Path.GetFileName(FilePath));
    formContent.Add(new StringContent("1"), "UserId");
    formContent.Add(new StringContent("1"), "Title");
    formContent.Add(new StringContent("1"), "Version");
    _httpClient.BaseAddress = new Uri("https://localhost:7233/");

    var response = await _httpClient.PostAsync("/api/addDocument", formContent);

    if (response.IsSuccessStatusCode)
    {
        
    }
}
CodingMytra
  • 2,234
  • 1
  • 9
  • 21
  • How can I do it using .net RestSharp library instead of using HttpClient class – mnu-nasir Jun 29 '22 at 11:22
  • check this [link](https://stackoverflow.com/a/59103382/9247039). this gives the idea how it can be done using restsharp and you have to adapt as per your model. – CodingMytra Jun 29 '22 at 11:41