コード例 #1
0
        protected override void Run(WP p, WD d)
        {
            StringWriter sw = new StringWriter();

            try
            {
                bool isSuccess = RunMe(p, d);
                //Mvc进行视图展示

                if (string.IsNullOrEmpty(d.RedirectUri))
                {
                    WMvcView.RenderView(p, d, CurrentContext, sw);
                    d.SetValue("ViewHtmlCode", sw.ToString());
                    if (!IsWebSocket)
                    {
                        CurrentContext.Response.Write(sw.ToString());
                    }
                    else
                    {
                        var v      = ComFunc.FormatJSON(sw.ToString());
                        var buffer = new ArraySegment <byte>(Encoding.UTF8.GetBytes(v.ToJSONString()));
                        CurrentSocket.SendAsync(buffer, WebSocketMessageType.Text, true, System.Threading.CancellationToken.None);
                    }
                }
                else
                {
                    AfterProcess(CurrentContext, p, d);
                    CurrentContext.Response.Redirect(d.RedirectUri, false);
                }
            }
            finally
            {
                sw.Flush();
                sw.Close();
                sw.Dispose();
                sw = null;
                p.Resources.ReleaseAll();
            }
        }
コード例 #2
0
ファイル: GoBaseHandler.cs プロジェクト: redwolf0817/EFFC3.0
        protected virtual void SetContent(WebParameter p, GoData d)
        {
            System.Collections.Specialized.NameValueCollection obj = CurrentContext.Request.Headers;
            String agent = ComFunc.nvl(obj["USER-AGENT"]);

            if (d.ResponseData == null)
            {
                throw new Exception("ResponseData is null!");
            }
            if (d.ContentType == GoResponseDataType.Pic_Jpg)
            {
                byte[] b = null;
                if (d.ResponseData is Stream)
                {
                    b = StreamToBytes((Stream)d.ResponseData);
                }
                else
                {
                    b = ((byte[])d.ResponseData);
                }

                CurrentContext.Response.AppendHeader("Content-Length", b.Length + "");
                CurrentContext.Response.ContentType = "image/jpeg";
                CurrentContext.Response.BinaryWrite(b);
            }
            else if (d.ContentType == GoResponseDataType.Pic_Gif)
            {
                byte[] b = null;
                if (d.ResponseData is Stream)
                {
                    b = StreamToBytes((Stream)d.ResponseData);
                }
                else
                {
                    b = ((byte[])d.ResponseData);
                }

                CurrentContext.Response.AppendHeader("Content-Length", b.Length + "");
                CurrentContext.Response.ContentType = "image/gif";
                CurrentContext.Response.BinaryWrite(b);
            }
            else if (d.ContentType == GoResponseDataType.Pic_Bmp)
            {
                byte[] b = null;
                if (d.ResponseData is Stream)
                {
                    b = StreamToBytes((Stream)d.ResponseData);
                }
                else
                {
                    b = ((byte[])d.ResponseData);
                }

                CurrentContext.Response.AppendHeader("Content-Length", b.Length + "");
                CurrentContext.Response.ContentType = "image/bmp";
                CurrentContext.Response.BinaryWrite(b);
            }
            else if (d.ContentType == GoResponseDataType.Pic_png)
            {
                byte[] b = null;
                if (d.ResponseData is Stream)
                {
                    b = StreamToBytes((Stream)d.ResponseData);
                }
                else
                {
                    b = ((byte[])d.ResponseData);
                }

                CurrentContext.Response.AppendHeader("Content-Length", b.Length + "");
                CurrentContext.Response.ContentType = "image/png";
                CurrentContext.Response.BinaryWrite(b);
            }
            else if (d.ContentType == GoResponseDataType.Excel)
            {
                CurrentContext.Response.Buffer  = true;
                CurrentContext.Response.Charset = "UTF-8";
                //CurrentContext.Response.ClearHeaders();
                CurrentContext.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(ComFunc.nvl(d["__download_filename__"]), Encoding.UTF8));
                CurrentContext.Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
                CurrentContext.Response.ContentType     = ResponseHeader_ContentType.xls;
                if (d.ResponseData is byte[])
                {
                    CurrentContext.Response.BinaryWrite((byte[])d.ResponseData);
                }
                else if (d.ResponseData is Stream)
                {
                    CurrentContext.Response.BinaryWrite(StreamToBytes((Stream)d.ResponseData));
                }
                else
                {
                    CurrentContext.Response.Write(d.ResponseData);
                }
                CurrentContext.Response.Flush();
            }
            else if (d.ContentType == GoResponseDataType.Word)
            {
                CurrentContext.Response.Buffer  = true;
                CurrentContext.Response.Charset = "UTF-8";
                //CurrentContext.Response.ClearHeaders();
                CurrentContext.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(ComFunc.nvl(d["__download_filename__"]), Encoding.UTF8));
                CurrentContext.Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
                CurrentContext.Response.ContentType     = ResponseHeader_ContentType.doc;
                if (d.ResponseData is byte[])
                {
                    CurrentContext.Response.BinaryWrite((byte[])d.ResponseData);
                }
                else if (d.ResponseData is Stream)
                {
                    CurrentContext.Response.BinaryWrite(StreamToBytes((Stream)d.ResponseData));
                }
                else
                {
                    CurrentContext.Response.Write(d.ResponseData);
                }
                CurrentContext.Response.Flush();
            }
            else if (d.ContentType == GoResponseDataType.PDF)
            {
                CurrentContext.Response.Buffer  = true;
                CurrentContext.Response.Charset = "UTF-8";
                //CurrentContext.Response.ClearHeaders();
                if (agent != null && agent.IndexOf("MSIE") == -1 && agent.IndexOf("Chrome") == -1 && agent.IndexOf("Opera") == -1)
                {
                    //非IE非Chrom
                    CurrentContext.Response.AppendHeader("Content-Disposition", "attachment;filename=" + ComFunc.nvl(d["__download_filename__"]));
                }
                else
                {
                    CurrentContext.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(ComFunc.nvl(d["__download_filename__"]), Encoding.UTF8));
                }

                CurrentContext.Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
                CurrentContext.Response.ContentType     = ResponseHeader_ContentType.pdf;
                if (d.ResponseData is byte[])
                {
                    CurrentContext.Response.BinaryWrite((byte[])d.ResponseData);
                }
                else if (d.ResponseData is Stream)
                {
                    CurrentContext.Response.BinaryWrite(StreamToBytes((Stream)d.ResponseData));
                }
                else
                {
                    CurrentContext.Response.Write(d.ResponseData);
                }
                CurrentContext.Response.Flush();
            }
            else if (d.ContentType == GoResponseDataType.Json)
            {
                CurrentContext.Response.Charset     = "UTF-8";
                CurrentContext.Response.ContentType = ResponseHeader_ContentType.json;
                if (d.ResponseData is FrameDLRObject)
                {
                    CurrentContext.Response.Write(ComFunc.FormatJSON((FrameDLRObject)d.ResponseData).ToJSONString());
                }
                else
                {
                    CurrentContext.Response.Write(ComFunc.FormatJSON(ComFunc.nvl(d.ResponseData)).ToJSONString());
                }
            }
            else if (d.ContentType == GoResponseDataType.HostView)
            {
                if (d.ResponseData is FrameDLRObject)
                {
                    var dobj = (FrameDLRObject)d.ResponseData;
                    //获取view路径
                    string viewpath = ComFunc.nvl(d.ExtentionObj.hostviewpath);
                    viewpath = viewpath.Replace("~", GlobalCommon.HostCommon.RootPath + HostJsConstants.COMPILED_VIEW_PATH);
                    if (File.Exists(viewpath))
                    {
                        //调用hostview引擎进行渲染
                        HostJsView hjv = (HostJsView)p.ExtentionObj.hostviewengine;
                        hjv.CurrentContext.SetDataModel(dobj.ToDictionary());
                        var html = hjv.Render(File.ReadAllText(viewpath, Encoding.UTF8));

                        CurrentContext.Response.Charset     = "UTF-8";
                        CurrentContext.Response.ContentType = ResponseHeader_ContentType.html;
                        CurrentContext.Response.Write(html);
                    }
                    else
                    {
                        CurrentContext.Response.Write("File Not Found!");
                    }
                }
            }
            else if (d.ContentType == GoResponseDataType.RazorView)
            {
                //Mvc进行视图展示
                StringWriter sw = new StringWriter();
                WMvcView.RenderView(p, d, CurrentContext, sw);
                d.SetValue("ViewHtmlCode", sw.ToString());
                if (!IsWebSocket)
                {
                    CurrentContext.Response.Charset     = "UTF-8";
                    CurrentContext.Response.ContentType = ResponseHeader_ContentType.html;
                    CurrentContext.Response.Write(sw.ToString());
                }
                else
                {
                    CurrentContext.Response.Charset     = "UTF-8";
                    CurrentContext.Response.ContentType = ResponseHeader_ContentType.json;
                    var v      = ComFunc.FormatJSON(sw.ToString());
                    var buffer = new ArraySegment <byte>(Encoding.UTF8.GetBytes(v.ToJSONString()));
                    CurrentSocket.SendAsync(buffer, WebSocketMessageType.Text, true, System.Threading.CancellationToken.None);
                }
            }
            else
            {
                CurrentContext.Response.Write(d.ResponseData);
            }
        }