I was trying to convert csv file to contentStream with below code.
private const string ResourceFolder = "TestData\\";
private HttpContent _form;
public void SendFile(string resource, string fileName)
{
_form = string.IsNullOrWhiteSpace(fileName)
? _form = new StringContent(string.Empty)
: _form = new StreamContent(File.OpenRead($"{ResourceFolder}{fileName}"));
var content = new MultipartFormDataContent();
content.Add(_form);
_form.Headers.ContentType = new MediaTypeHeaderValue("application/csv");
WhenThePostRequestExecutesWithContent(resource, content);
}
public async void WhenThePostRequestExecutesWithContent(string resource, HttpContent content)
{
ResponseMessage = await HttpClient.PostAsync(resource, content);
}
I am using .Net core 2.1
and it gives below error in the last line in File
location
The problem is I still find null for the below controller file parameter,
Controller:
public async Task<IActionResult> SeedData(IFormFile file)
{
var result = await _seedDataService.SeedData(file);
return Ok(new { IsUploadSuccesful = result});
}
Thanks to everyone's contribution, Below code solved the problem.
private const string ResourceFolder = "TestData\\";
private HttpContent _form;
public void AttachedRatesFile(string fileName)
{
_form = string.IsNullOrWhiteSpace(fileName)
? _form = new StringContent(string.Empty)
: _form = new StreamContent(File.OpenRead($"{ResourceFolder}{fileName}"));
_content = new MultipartFormDataContent();
_content.Add(_form, "file", fileName);
_form.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
}
public async void WhenThePostRequestExecutesWithContent(string resource, HttpContent content)
{
ResponseMessage = await HttpClient.PostAsync(resource, content);
}