Re: Javascript Error When Uploading

  •  03-27-2009, 3:35 PM

    Re: Javascript Error When Uploading

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.IO;
    using System.Text;

    public class MarksMoveViewStateMod : IHttpModule
    {  

        void IHttpModule.Init(HttpApplication context)
        {
            // Add Event
            context.BeginRequest += new EventHandler(context_BeginRequest);
        }

        void context_BeginRequest(object sender, EventArgs e)
        {
            HttpApplication _HttpApp = sender as HttpApplication;
            if (_HttpApp.Request.Url.OriginalString.Contains(".aspx"))
            {
                _HttpApp.Response.Filter = new ViewstateFilter(_HttpApp.Response.Filter);
            }
        }

        private class ViewstateFilter : Stream
        {
            public ViewstateFilter(Stream TempStream)
            {
                _TmpStream = TempStream;
            }

            public override bool CanRead { get { return true; } }

            public override bool CanSeek { get { return true; } }

            public override bool CanWrite { get { return true; } }

            public override void Flush() { _TmpStream.Flush(); }

            public override long Length { get { return 0; } }

            public override long Position
            {
                get { return _position; }
                set { _position = value; }
            }
            public override int Read(byte[] buffer, int offset, int count) { return _TmpStream.Read(buffer, offset, count); }
            public override long Seek(long offset, SeekOrigin origin) { return _TmpStream.Seek(offset, origin); }
            public override void SetLength(long value) { _TmpStream.SetLength(value); }
            public override void Close() { _TmpStream.Close(); }
            private StringBuilder _DataBuffer = new StringBuilder();
            private Stream _TmpStream;
            private long _position;

            public override void Write(byte[] buffer, int offset, int count)
            {

                string html_part = System.Text.Encoding.Default.GetString(buffer);

                _DataBuffer.Append(html_part);

                // I bet this causes problems with something...  but No definitive way to test for this unless EOF is sent
                // No time to test....
                if (html_part.Contains("</html>"))
                {
                    string html = _DataBuffer.ToString();
                    html = html.Replace("~/", Utility.BASEURL);
                    int startPoint = html.IndexOf("<input type=\"hidden\" name=\"__VIEWSTATE\"");
                    // Can Add Other ASP.NEt *** here also
                    if (startPoint >= 0)
                    {
                        int endPoint = html.IndexOf("/>", startPoint) + 2;
                        string viewstateInput = html.Substring(startPoint, endPoint - startPoint);
                        html = html.Remove(startPoint, endPoint - startPoint);
                        int formEndStart = html.IndexOf("</form>") - 1;
                        if (formEndStart >= 0) { html = html.Insert(formEndStart, viewstateInput); }
                    }
                    byte[] outdata = System.Text.Encoding.Default.GetBytes(html);
                    _TmpStream.Write(outdata, 0, outdata.GetLength(0));
                }
            }
        }    

        void IHttpModule.Dispose()
        {
          
        }

    }

View Complete Thread