File upload – simple multipart request stream parser, using Regex

· C#, HTML5

(In case your text file contains line starting exactly with the “matched” ones below – simply adapt your Regex)

   private static List<string> fileUploadRequestParser(Stream stream)
        //Content-Disposition: form-data; name="file"; filename="data.txt"
        //Content-Type: text/plain
        //Content-Disposition: form-data; name="submit"

        List<String> lstLines = new List<string>();
        TextReader textReader = new StreamReader(stream);
        string sLine = textReader.ReadLine();
        Regex regex = new Regex("(^-+)|(^content-)|(^$)|(^submit)", RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.Singleline);

        while (sLine != null)
            if (!regex.Match(sLine).Success)
            sLine = textReader.ReadLine();

        return lstLines;

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: