Trying to get bytes from this encoded string (image)

236 Views Asked by At

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

but the image doesn't render.Strying I get when reading body value

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?

1

There are 1 best solutions below

0
On

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:

using (var httpResponse = (HttpWebResponse)request.GetResponse())
                using (Stream responseStream = httpResponse.GetResponseStream())
                {
                    MediaTypeHeaderValue contentType = MediaTypeHeaderValue
                        .Parse(request.ContentType);

                    string boundary = HeaderUtilities
                        .RemoveQuotes(contentType.Boundary).Value;

                    MultipartSection section;

                    for (MultipartReader smth = new(boundary, responseStream); ;)
                    {
                        section = await smth.ReadNextSectionAsync();

                        if (section is null) break;

                        var bytes = ConverteStreamToByteArray(section.Body);
                        
                        // Save or handle the Byte array
                    }
                }

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

 public static byte[] ConvertStreamToByteArray(Stream stream)
        {
            stream.Position = 0; 
            // for some reason the position aways starts at the last index, So make  
            // sure to set it to 0 here

            byte[] byteArray = new byte[16 * 1024];
            using (MemoryStream mStream = new())
            {
                int bit;
                while ((bit = stream.Read(byteArray, 0, byteArray.Length)) > 0)
                {
                    mStream.Write(byteArray, 0, bit);
                }
                return mStream.ToArray();
            }
        }