How do you post a file with HttpClient? It can't be that hard, can it?
I am posting a file from a Windows forms client to an ASP.Net Core 2.2 website API (not WebApi)
The file could be anything, word, pdf, image, video etc... The file could be anything up to 500MB and my JSON methods won't send anything above 25MB
No matter what I do I keep getting
StatusCode: 415, ReasonPhrase: 'Unsupported Media Type'
I can't work out what's gong wrong I have no idea what's missing. I've narrowed it down to this
string filepath = file;
string filename = Path.GetFileName(file);
MultipartFormDataContent content = new MultipartFormDataContent();
var fileContent = new StreamContent(File.OpenRead(filepath));
fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data") { FileName = $"\"{filename}\"" };
content.Add(fileContent);
HttpResponseMessage response = await _httpClient.PostAsync(serviceMethod, content);
All the examples I've read (loads of them), most seem to be sensing JSON and I can do that in spades. The rest tell you what to read and look for but I'm still lost I just want to post a file. I have the server code ready to go.
[HttpPost]
public async Task<JsonResult> UploadFile([FromForm]IFormFile result)
I'll keep reading but any help would be much appreciated.
Ok, I got a bit closer. I've updated my code (see above) and now the controller on my API is getting invoked but now result
is null