I have a project implemented in .NET 4.5, and for some reason, I cannot change it. I want to post a photo to a web API that is implemented with .NET Core. The API endpoint uses HTTP POST, and its input type is generic ICollection that its type is IFormFile. However, I am unable to send even one picture because I can only send byte types to the API. How can I solve this problem? Here is my code:
using (HttpClient client = new HttpClient())
{
client.BaseAddress = new Uri(ServiceInfo.VIPCParams["DMSUri"].Value);
client.DefaultRequestHeaders.Accept.Clear();
// client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", responseModelLogin.Token);
// string json = JsonConvert.SerializeObject(parameters);
// HttpContent content = new ByteArrayContent(uploadRequest.File);
HttpContent content = new ByteArrayContent(uploadRequest.File);
//HttpContent content = new StreamContent(stream);
//content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
//using (var stream = new MemoryStream(uploadRequest.File))
//{
// await documentRepository.UploadFile(stream);
//}
// content.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/form-data");
var multipartContent = new MultipartFormDataContent();
multipartContent.Add(content);
// multipartContent.Add(new StreamContent(content));
HttpResponseMessage res = client.PostAsync(url, multipartContent).Result;
string data = res.Content.ReadAsStringAsync().Result;
mSUploadResponse = JsonConvert.DeserializeObject<DMSUploadResponse>(data);
}