Ejemplo n.º 1
0
 /// <summary>
 /// Extracts files from the http request stream.
 /// </summary>
 /// <param name="input">Http request stream (body).</param>
 /// <returns>Collection of uploaded files.</returns>
 public static ICollection <UploadedFile> ExtractFiles(Stream input)
 {
     using (HttpUploadStream stream = new HttpUploadStream(input, Encoding.UTF8))
     {
         return(ParseStream(stream));
     }
 }
Ejemplo n.º 2
0
        private static int FindToken(byte[] array, byte[] token, int arrayOffset)
        {
            if (array == null)
            {
                throw new ArgumentNullException("array", "Value cannot be null.");
            }

            if (token == null)
            {
                throw new ArgumentNullException("token", "Value cannot be null.");
            }

            for (int i = arrayOffset; i < array.Length; i++)
            {
                if (HttpUploadStream.CompareArray(array, token, i, 0, token.Length))
                {
                    return(i - 2); //2 bytes for newline
                }
            }

            return(-1);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Reads the file data.
        /// </summary>
        /// <param name="token">The token for the data.</param>
        /// <returns></returns>
        public byte[] ReadFileData(string token)
        {
            long startingPos = this.Position;

            byte[] delimiterBytes = this.encoding.GetBytes(token);

            int endPos = HttpUploadStream.FindToken(this.ms.ToArray(), delimiterBytes, (int)startingPos);

            int filesize = endPos - (int)startingPos;

            if (filesize > 0)
            {
                byte[] fileData = new byte[filesize];

                this.Position = startingPos;
                this.Read(fileData, 0, fileData.Length);

                return(fileData);
            }
            else
            {
                return(null);
            }
        }
Ejemplo n.º 4
0
        private static ICollection <UploadedFile> ParseStream(HttpUploadStream stream)
        {
            List <UploadedFile> files = new List <UploadedFile>();

            string token    = stream.ReadLine();
            string endToken = token + "--";

            stream.Position = 0;

            bool   wasContentDisposition = false;
            bool   wasContentType        = false;
            bool   wasToken = false;
            string filename = null;

            byte[] fileData = null;

            while (!stream.IsEndOfStream)
            {
                if (!wasContentType || !wasContentDisposition || !wasToken) //process headers
                {
                    string line = stream.ReadLine();

                    if (line == null || line == endToken)
                    {
                        break;
                    }
                    else if (line == token)
                    {
                        wasToken = true;
                    }
                    else if (line.StartsWith("Content-Disposition", StringComparison.Ordinal))
                    {
                        wasContentDisposition = true;
                        int fIndex = line.IndexOf("filename=\"", StringComparison.Ordinal);

                        if (fIndex >= 0)
                        {
                            filename = line.Substring(fIndex + 10, line.IndexOf('\"', fIndex + 10) - fIndex - 10);
                        }
                    }
                    else if (line.StartsWith("Content-Type", StringComparison.Ordinal))
                    {
                        wasContentType = true;
                    }
                    else
                    {
                        wasContentDisposition = false;
                        wasContentType        = false;
                        wasToken = false;
                        filename = null;
                    }
                }
                else //get the file data
                {
                    fileData = stream.ReadFileData(token);

                    if (fileData != null)
                    {
                        UploadedFile file = new UploadedFile();
                        file.Data = fileData;

                        if (filename.IndexOf('\\') >= 0)
                        {
                            filename = filename.Substring(filename.LastIndexOf('\\') + 1);
                        }

                        file.FileName = filename;
                        files.Add(file);
                    }

                    wasContentDisposition = false;
                    wasContentType        = false;
                    wasToken = false;
                    filename = null;
                }
            }

            return(files);
        }