passing a file in to an Core 2.1 WebApi method
-
I have the following API Controller with a simple method to accept a file via the IFormfile, which works a treat in Postman. Now I need to call this with a
HttpClient
call from a console application.[HttpPost]
public IActionResult PostFile(IFormFile file)
{
//Do Stuff
Return OK();
}This is where I am stuck can anyone give me a good couple of examples of creating a
PostAsync
call so that I can transfer a file to the server? Thanks SimonEvery day, thousands of innocent plants are killed by vegetarians. Help end the violence EAT BACON
-
I have the following API Controller with a simple method to accept a file via the IFormfile, which works a treat in Postman. Now I need to call this with a
HttpClient
call from a console application.[HttpPost]
public IActionResult PostFile(IFormFile file)
{
//Do Stuff
Return OK();
}This is where I am stuck can anyone give me a good couple of examples of creating a
PostAsync
call so that I can transfer a file to the server? Thanks SimonEvery day, thousands of innocent plants are killed by vegetarians. Help end the violence EAT BACON
Something like this should work:
public static async Task UploadFile(HttpClient client, string url, string filePath, string contentType, CancellationToken cancellationToken)
{
// Argument validation omitted...using (var fileStream = File.OpenRead(filePath)) { var file = new StreamContent(fileStream); file.Headers.ContentType = MediaTypeHeaderValue.Parse(mimeType); file.Headers.ContentLength = fileStream.Length; var content = new MultipartFormDataContent(); content.Add(file, "file", Path.GetFileName(filePath)); using (var response = await client.PostAsync(url, content, cancellationToken).ConfigureAwait(false)) { response.EnsureSuccessStatusCode(); } }
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Something like this should work:
public static async Task UploadFile(HttpClient client, string url, string filePath, string contentType, CancellationToken cancellationToken)
{
// Argument validation omitted...using (var fileStream = File.OpenRead(filePath)) { var file = new StreamContent(fileStream); file.Headers.ContentType = MediaTypeHeaderValue.Parse(mimeType); file.Headers.ContentLength = fileStream.Length; var content = new MultipartFormDataContent(); content.Add(file, "file", Path.GetFileName(filePath)); using (var response = await client.PostAsync(url, content, cancellationToken).ConfigureAwait(false)) { response.EnsureSuccessStatusCode(); } }
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Richard, Thanks for this and apologies that I have taken so long to respond, had to tweak your solution slightly to make it work
using (var formData = new MultipartFormDataContent())
{
Task responseMessage = null;
using (var stream = File.OpenRead(pdf))
{
HttpContent fileStreamContent = new StreamContent(stream);
fileStreamContent.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("form-data")
{ Name = "DigitalPCN", FileName = Path.GetFileName(fine.FileName) };
fileStreamContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/pdf");
formData.Add(fileStreamContent);responseMessage = base.PostAsync(new Uri(@"https://localhost:44349/api/Controller/Method"), formData, token); responseMessage.Wait(); } return responseMessage.Result;
}
Every day, thousands of innocent plants are killed by vegetarians. Help end the violence EAT BACON