I have tried to send file through multipart form content. The code in API like shown below:
app.MapGet("/GetFile", () =>
{
MultipartFormDataContent multiPartContent = new MultipartFormDataContent("----MyGreatBoundary");
multiPartContent.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/form-data; boundary=----MyGreatBoundary");
HttpContent content1 = new ByteArrayContent(File.ReadAllBytes(@"C:/temp/TimeAkis.txt"));
content1.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("TimeAkis");
content1.Headers.ContentDisposition.FileName = "TimeAkis.txt";
multiPartContent.Add(content1);
return multiPartContent;
});
While accessing the above api method through a console application. We couldn't get the 'MultiPartContent'.
The code shown like below:
internal async Task Read()
{
HttpClient httpClient = new HttpClient();
Uri webService = new Uri(@"https://localhost:44324/GetFile");
HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Get, webService);
requestMessage.Headers.ExpectContinue = false;
Task<HttpResponseMessage> httpRequest = httpClient.GetAsync(webService);
HttpResponseMessage responseMessage = httpRequest.GetAwaiter().GetResult();
string strContentType = responseMessage.Content.Headers.ContentType?.MediaType;
if(strContentType != null && IsMultipartContentType(strContentType))
{
try
{
var multipartcontent = await responseMessage.Content.ReadAsMultipartAsync();
foreach(HttpContent content in multipartcontent.Contents)
{
}
}
catch(Exception e)
{
Console.WriteLine(e);
}
}
}
It returns the content type as 'Json':
Please help us to get the multipart data. (OR)
Am I wrong in creating API with multipart?.
Please suggest to me.
Thanks.