Example #1
0
        ///Сохранить текст сообщения от клиента в файл. Сейчас не используется.
        public static string GetRequestFile(Encoding enc, string boundary, Stream input, string FileName)
        {
            Byte[] boundaryBytes = enc.GetBytes(boundary);
            Int32  boundaryLen   = boundaryBytes.Length;
            string TempPath;

            if (!FBAFile.GetPathTemp(out TempPath))
            {
                return("error creating temporary directory: " + TempPath);
            }
            string FullFileName = TempPath + FileName;

            using (var output = new FileStream(FullFileName, FileMode.Create, FileAccess.Write))
            {
                var   buffer   = new Byte[1024];
                Int32 len      = input.Read(buffer, 0, 1024);
                Int32 startPos = -1;

                //Find start boundary
                while (true)
                {
                    if (len == 0)
                    {
                        return("Start Boundaray Not Found");
                    }

                    startPos = IndexOf(buffer, len, boundaryBytes);
                    if (startPos >= 0)
                    {
                        break;
                    }
                    else
                    {
                        Array.Copy(buffer, len - boundaryLen, buffer, 0, boundaryLen);
                        len = input.Read(buffer, boundaryLen, 1024 - boundaryLen);
                    }
                }

                //Skip four lines (Boundary, Content-Disposition, Content-Type, and a blank)
                for (Int32 i = 0; i < 4; i++)
                {
                    while (true)
                    {
                        if (len == 0)
                        {
                            return("Preamble not Found.");
                        }

                        startPos = Array.IndexOf(buffer, enc.GetBytes("\n")[0], startPos);
                        if (startPos >= 0)
                        {
                            startPos++;
                            break;
                        }
                        else
                        {
                            len = input.Read(buffer, 0, 1024);
                        }
                    }
                }

                Array.Copy(buffer, startPos, buffer, 0, len - startPos);
                len = len - startPos;

                while (true)
                {
                    Int32 endPos = IndexOf(buffer, len, boundaryBytes);
                    if (endPos >= 0)
                    {
                        if (endPos > 0)
                        {
                            output.Write(buffer, 0, endPos - 2);
                        }
                        break;
                    }
                    else if (len <= boundaryLen)
                    {
                        return("End Boundaray Not Found");
                    }
                    else
                    {
                        output.Write(buffer, 0, len - boundaryLen);
                        Array.Copy(buffer, len - boundaryLen, buffer, 0, boundaryLen);
                        len = input.Read(buffer, boundaryLen, 1024 - boundaryLen) + boundaryLen;
                    }
                }
            }
            return("Success");
        }