Ejemplo n.º 1
0
        /// <summary>
        /// Processes all upload controls in a control collection when the module is not installed.
        /// </summary>
        /// <param name="cc">Control collection.</param>
        /// <param name="defaultProcessor">The default processor.</param>
        /// <param name="status">The upload status.</param>
        void ProcessUploadControls(ControlCollection cc, IFileProcessor defaultProcessor, UploadStatus status)
        {
            foreach (Control c in cc)
            {
                System.Web.UI.WebControls.FileUpload fu = c as System.Web.UI.WebControls.FileUpload;

                if (fu != null && fu.HasFile)
                {
                    IFileProcessor controlProcessor = GetProcessorForControl(fu);
                    IFileProcessor processor        = controlProcessor == null ? defaultProcessor : controlProcessor;

                    try
                    {
                        processor.StartNewFile(fu.FileName, fu.PostedFile.ContentType, null, null);
                        processor.Write(fu.FileBytes, 0, fu.FileBytes.Length);
                        processor.EndFile();

                        status.UploadedFiles.Add(new UploadedFile(fu.FileName, processor.GetIdentifier(), null));
                    }
                    catch (Exception ex)
                    {
                        status.ErrorFiles.Add(new UploadedFile(fu.FileName, processor.GetIdentifier(), null, ex));
                    }
                }

                if (c.HasControls())
                {
                    ProcessUploadControls(c.Controls, defaultProcessor, status);
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Writes data to the stream.
        /// </summary>
        /// <param name="bytes">Buffer to write from.</param>
        /// <param name="offset">Offset in the buffer to write from.</param>
        /// <param name="count">Count of the bytes to write.</param>
        public override void Write(byte[] bytes, int offset, int count)
        {
            byte[] input;
            int    start = 0;
            int    end   = 0;

            // System.Diagnostics.Debug.WriteLine("Entering write");

            if (_buffer != null)
            {
                input = new byte[_buffer.Length + count];
                Buffer.BlockCopy(_buffer, 0, input, 0, _buffer.Length);
                Buffer.BlockCopy(bytes, offset, input, _buffer.Length, count);
            }
            else
            {
                input = new byte[count];
                Buffer.BlockCopy(bytes, offset, input, 0, count);
            }

            _position += count;

            while (true)
            {
                if (_headerNeeded)
                {
                    int prevStart = start;

                    start = IndexOf(input, BOUNDARY, start);

                    if (start >= 0)
                    {
                        end = IndexOf(input, EOF, start);

                        if (end == start)
                        {
                            // This is the end of the stream
                            WriteBytes(false, input, start, input.Length - start);
                            // System.Diagnostics.Debug.WriteLine("EOF");
                            break;
                        }

                        // Do we have the whole header?
                        end = IndexOf(input, EOH, start);

                        if (end >= 0)
                        {
                            Dictionary <string, string> headerItems;

                            // We have a full header
                            _inField      = true;
                            _headerNeeded = false;

                            headerItems = ParseHeader(input, start);

                            if (headerItems == null)
                            {
                                throw new Exception("Malformed header");
                            }

                            if (headerItems.ContainsKey("filename") && headerItems.ContainsKey("Content-Type"))
                            {
                                string fn = headerItems["filename"].Trim('"').Trim();

                                if (!String.IsNullOrEmpty(fn))
                                {
                                    try
                                    {
                                        object id;

                                        _fileName = headerItems["filename"].Trim('"');
                                        _inFile   = true;
                                        id        = _processor.StartNewFile(fn, headerItems["Content-Type"], headerItems, _previousFields);

                                        OnFileStarted(fn, id);
                                    }
                                    catch (Exception ex)
                                    {
                                        _fileError = true;
                                        OnError(ex);
                                    }
                                }
                            }
                            else
                            {
                                _inFile           = false;
                                _currentField     = new MemoryStream();
                                _currentFieldName = headerItems["name"];
                            }

                            start = end + 4;
                        }
                        else
                        {
                            _buffer = new byte[input.Length - start];
                            Buffer.BlockCopy(input, start, _buffer, 0, input.Length - start);
                            // System.Diagnostics.Debug.WriteLine("Breaking because no header found (2)");
                            break;
                        }
                    }
                    else
                    {
                        _buffer = new byte[input.Length - prevStart];
                        Buffer.BlockCopy(input, prevStart, _buffer, 0, input.Length - prevStart);
                        // System.Diagnostics.Debug.WriteLine("Breaking because no header found (1)");
                        break;
                    }
                }

                SectionResult res = null;

                if (_inField)
                {
                    _buffer = null; // Reset the buffer

                    // Process data
                    res = ProcessField(input, start);

                    if (res.NextAction == SectionResult.SectionAction.BoundaryReached)
                    {
                        // System.Diagnostics.Debug.WriteLine("Found a new boundary");
                        _headerNeeded = true;
                        _inField      = false;
                        start         = res.NextOffset;

                        if (_inFile)
                        {
                            _inFile = false;

                            try
                            {
                                _processor.EndFile();
                            }
                            catch (Exception ex)
                            {
                                OnError(ex);
                                _fileError = true;
                            }
                            finally
                            {
                                if (_fileError)
                                {
                                    OnFileCompletedError(_fileName, _processor.GetIdentifier(), _ex);
                                }
                                else
                                {
                                    OnFileCompleted(_fileName, _processor.GetIdentifier());
                                }
                            }
                        }
                    }
                    else if (res.NextAction == SectionResult.SectionAction.NoBoundaryKeepBuffer)
                    {
                        // System.Diagnostics.Debug.WriteLine("Keeping back " + (input.Length - res.NextOffset).ToString() + " bytes");
                        _buffer = new byte[input.Length - res.NextOffset];
                        Buffer.BlockCopy(input, res.NextOffset, _buffer, 0, input.Length - res.NextOffset);
                        break;
                    }
                }

                if (!_headerNeeded && !_inField)
                {
                    throw new Exception("Malformed input file- don't know what to do so aborting.");
                }
            }

            // System.Diagnostics.Debug.WriteLine("Leaving write");
        }