Response.Filter实现繁简转换

http://www.1task.cn/blog/article/60.aspx
文件名乱码问题 : http://www.1task.cn/blog/article/5.aspx
using System;
using System.IO;
using System.Text;

namespace  Sample
{
 public class ResponseFilter : Stream
 {
  private Stream m_sink;
  private long m_position;

  public ResponseFilter(Stream sink)
  {
   m_sink = sink;
  }

  // The following members of Stream must be overriden.
  public override bool CanRead
  {get { return true; }}

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

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

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

  public override long Position
  {
   get { return m_position; }
   set { m_position = value; }
  }

  public override long Seek(long offset, System.IO.SeekOrigin direction)
  {
   return 0;
  }

  public override void SetLength(long length)
  {
   m_sink.SetLength(length);
  }

  public override void Close()
  {
   m_sink.Close();
  }

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

  public override int Read(byte[] buffer, int offset, int count)
  {
   return m_sink.Read(buffer, offset, count);
  }

  // Override the Write method to filter Response to a file.
  public override void Write(byte[] buffer, int offset, int count)
  {
   string s = Microsoft.VisualBasic.Strings.StrConv(
    Encoding.GetEncoding("GB2312")..GetString(buffer),
    Microsoft.VisualBasic.VbStrConv.TraditionalChinese,0
    );
   byte[] b = Encoding.GetEncoding("BIG5").GetBytes(s);
   m_sink.Write(b, 0, count);

  }
 }
}

2、在Global.Application_BeginRequest使用Filter
this.Response.Charset = "BIG5";
this.Response.Buffer = false;
this.Response.Filter = new ResponseFilter(Response.Filter);

3、完成。

posted @ 2008-05-22 11:01  winnerlan  阅读(259)  评论(0编辑  收藏  举报