0

hello I'm currently making a server application that accepts uploads via a web page, issue is the uploaded file, is a multipart form-data file and I do not know how I should go about handling the multipart file,

example file :

-----------------------------231197858634295637401474252101
Content-Disposition: form-data; name="Uploadfile"; filename="steam.desktop"
Content-Type: application/x-desktop

and at the end,


-----------------------------231197858634295637401474252101--

this is data I do not want as they are not useful to me, how would I go about getting what the user uploaded.

right now I'm just using whatever random text file I have on my desktop so I can have a readable output but the plan is to be able to upload Images

I am using the httplistener class and Mono

Pixel
  • 16
  • 3
  • Welcome to SO! It is better to use a [minimally reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). Anyway, I'm assuming that this a Header that you received through POST. Post - no pun intended - any code that you already tried. – Marcelo Scofano Diniz Oct 18 '22 at 18:23
  • @MarceloScofanoDiniz Hi thanks for the response, i did not try any code as I'm not sure on how to handle this, this is the body(?) of a file upload form however this is the code used to store the data ```var istream = req.InputStream; req.InputStream.CopyTo(isteam); byte[] bodycontent8 = isteam.ToArray();``` I'm sorry I do not know why the code is not on separate lines, – Pixel Oct 18 '22 at 18:32
  • See if [this](https://stackoverflow.com/questions/3794358/how-to-parse-http-postfile-upload-stream) helps you to understand how to parse it. PS: it is ok, comments does not allow that we separate lines. – Marcelo Scofano Diniz Oct 18 '22 at 18:44
  • Take a look [here](https://stackoverflow.com/questions/7460088/reading-file-input-from-a-multipart-form-data-post), [here](https://stackoverflow.com/questions/26529797/httplistener-post-form-data) and [here](https://stackoverflow.com/questions/48050370/get-posted-file-over-http-listener-in-c-sharp) – Marcelo Scofano Diniz Oct 18 '22 at 18:49

1 Answers1

0

managed to get it working with this code

if (req.HttpMethod == "POST")
        {
            if (req.ContentType.Contains("multipart/form-data"))
            {
                // remove header of multipart form data
                Array.Copy(bodycontent8, Soarr.SearchBytes(bodycontent8, tofind) + 4 ,bodycontent8, 0 ,bodycontent8.Length - Soarr.SearchBytes(bodycontent8, tofind) - 4);

                // remove footer of multipart form data
                byte[] Boundary = encoding.GetBytes(req.ContentType.Split(';')[1].Split('=')[1]);
                Array.Resize(ref bodycontent8, Soarr.SearchBytes(bodycontent8, Boundary) - 4);
            }
        }

__ Soarr.SearchBytes

public static int SearchBytes(byte[] haystack, byte[] needle)
    {
        var len = needle.Length;
        var limit = haystack.Length - len;
        for (var i = 0; i <= limit; i++)
        {
            var k = 0;
            for (; k < len; k++)
            {
                if (needle[k] != haystack[i + k]) break;
            }
            if (k == len) return i;
        }
        return -1;
    }

tofind is { 13,10,13,10 }

I needed to remove stuff added for the multipart form data format, this worked for me, alto I don't believe it to the best solution.

Pixel
  • 16
  • 3
  • Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, **can you [edit] your answer to include an explanation of what you're doing** and why you believe it is the best approach? – Jeremy Caney Nov 10 '22 at 00:31