I'm trying to get an image from a multipart-form/data in my .net core API, but I just cant figure out what encoding is being use to encrypt this image data. Basically I need to get the byte array (of the image) represented inside this string
and this is how I get this encoded string:
using (Stream responseStream = resposta.GetResponseStream())
{
var contentType = MediaTypeHeaderValue.Parse(response.ContentType);
var boundary = HeaderUtilities.RemoveQuotes(contentType.Boundary).Value;
for (MultipartReader smth = new(boundary, responseStream); ;)
{
try
{
MultipartSection section = await smth.ReadNextSectionAsync();
if (section == null)
break;
string contentTypeFrame = section.ContentType;
// Returns me the encoded string
string bodyValue = await section.ReadAsStringAsync();
if (bodyValue.ToLower().Contains("heartbeat"))
continue;
if (contentTypeFrame == "image/jpeg")
{
//Do something if it is an image
}
}
catch (Exception ex) { }
}
}
any ideas on how'd I try to decode this string to get the image bytes?
I figured it out.
after many tries I got the correct piece of code to handle getting files from "multipart/x-mixed-replace;" requests, Once you break it to the MultiPart Section here:
OBS: If you're really looking for the bytes of the content, under no circumstances use the readasstringasync() function, it'll change the content body. (In case of images, it'll add both the content-type and content-length to the body, and you wont be able to convert it to jpg again)
You'll now only need this function I wrote to get the data out of the Section Body Stream