2008年12月

无边的select,去掉select的蓝边

有时候觉得xp上select的默认蓝边很难看,使用以下语句可以去掉select的边,其实是把它隐藏了
<span style="border:1px solid #ccc;position:absolute;overflow:hidden">
 <select style="margin:-1px;">
  <option>这个东西看起来更简单</option>
  <option>但是总是要求position:absolute</option>
  <option>还要注意:select & span 要写在一行上</option>
 </select>
</span>
span必须加上position:absolute才行,但好像又不会绝对定位,ie测试通过

点击后直接弹出保存文件的对话框,而不用弹出新窗口

一般的直接下载文件的代码如下:
System.IO.FileInfo file = new System.IO.FileInfo("F:\\mp3\\mp3\\茉莉花.mp3");
Response.Clear();
Response.ClearHeaders();
Response.Buffer = false;
Response.Charset="GB2312";
Response.ContentEncoding=System.Text.Encoding.UTF8;
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename="+Server.UrlEncode("茉莉花.mp3"));
Response.AddHeader("Content-Length", file.Length.ToString());
Response.WriteFile(file.FullName);
Response.Flush();
Response.End();

另一种对于大文件,我们用上述代码不是较好,因为上述代码是将文件全缓存于服务器后才输出的,如果你的服务器流量大,且文件较大,不建议用以上方法,下面的代码其实就是把文件进行了分隔,慢慢的输出到客户端!
public static bool ResponseFile(HttpRequest _Request,HttpResponse _Response,string _fileName,string _fullPath, long _speed)
  {
   try
   {
    FileStream myFile = new FileStream(_fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
    BinaryReader br = new BinaryReader(myFile);
    try
    {
     _Response.AddHeader("Accept-Ranges", "bytes");
     _Response.Buffer = false;
     long fileLength = myFile.Length;
     long startBytes = 0;
    
     int pack = 10240; //10K bytes
     int sleep = (int)Math.Floor(1000 * pack / _speed) + 1;
     if (_Request.Headers["Range"] != null)
     {
      _Response.StatusCode = 206;
      string[] range = _Request.Headers["Range"].Split(new char[] {'=', '-'});
      startBytes = Convert.ToInt64(range[1]);
     }
     _Response.AddHeader("Content-Length", (fileLength - startBytes).ToString());
     if (startBytes != 0)
     {
      _Response.AddHeader("Content-Range", string.Format(" bytes {0}-{1}/{2}", startBytes, fileLength-1, fileLength));
     }
     _Response.AddHeader("Connection", "Keep-Alive");
     _Response.ContentType = "application/octet-stream";
     _Response.AddHeader("Content-Disposition","attachment;filename=" + HttpUtility.UrlEncode(_fileName,System.Text.Encoding.UTF8) );
 
     br.BaseStream.Seek(startBytes, SeekOrigin.Begin);
     int maxCount = (int) Math.Floor((fileLength - startBytes) / pack) + 1;

 

     for (int i = 0; i < maxCount; i++)
     {
      if (_Response.IsClientConnected)
      {
       _Response.BinaryWrite(br.ReadBytes(pack));
       Thread.Sleep(sleep);
      }
      else
      {
       i=maxCount;
      }
     }
     _Response.Flush();
    }
    catch
    {
     return false;
    }
    finally
    {
     br.Close();
     myFile.Close();
    }
   }
   catch
   {
    return false;
   }
   return true;
  }

说明:
 _Response.Flush();把当前缓冲区中的数据输出到客户端

C# 用VB.net 函数库来实现全角与半角转换、简体和繁体转换

        /// 转全角的函数(SBC case)
        ///
        /// 任意字符串
        /// 全角字符串
        ///
        ///全角空格为12288,半角空格为32
        ///其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248
        ///       
        public string ToSBC(string input)
        {
            //半角转全角:
            char[] c=input.ToCharArray();
            for (int i = 0; i < c.Length; i++)
            {
                if (c[i]==32)
                {
                    c[i]=(char)12288;
                    continue;
                }
                if (c[i]<127)
                    c[i]=(char)(c[i]+65248);
            }
            return new string(c);               
        }


        /**////
        /// 转半角的函数(DBC case)
        ///
        /// 任意字符串
        /// 半角字符串
        ///
        ///全角空格为12288,半角空格为32
        ///其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248
        ///
        public string ToDBC(string input)
        {   
            char[] c=input.ToCharArray();
            for (int i = 0; i < c.Length; i++)
            {
                if (c[i]==12288)
                {
                   c[i]= (char)32;
                    continue;
                }
                if (c[i]>65280 && c[i]<65375)
                    c[i]=(char)(c[i]-65248);
            }   
            return new string(c);
        }
2.C#中直接调用VB.NET的函数,兼论半角与全角、简繁体中文互相转化

在C#项目中添加引用Microsoft.VisualBasic.dll, 可以在C#程序中直接使用VB.NET中丰富的函数 
 

 // 命令行编译 : csc /r:Microsoft.VisualBasic.dll Test.cs
 // 如果是用 Visual Studio .NET IDE, 请按以下方法为项目添加引用:
 // 打开[解决方案资源管理器], 右击项目名称, 选择[添加引用],
 // 从列表中选择 Microsoft Visual Basic .NET Runtime 组件.

 using Microsoft.VisualBasic;

class Test
{
  static void Main()
  {
    string s = "空军]";
    System.Console.WriteLine(s);
    s = Strings.StrConv(s, VbStrConv.Wide              , 0); // 半角转全角
    s = Strings.StrConv(s, VbStrConv.TraditionalChinese, 0); // 简体转繁体
    System.Console.WriteLine(s);
    s = Strings.StrConv(s, VbStrConv.ProperCase        , 0); // 首字母大写
    s = Strings.StrConv(s, VbStrConv.Narrow            , 0); // 全角转半角
    s = Strings.StrConv(s, VbStrConv.SimplifiedChinese , 0); // 繁体转简体
    System.Console.WriteLine(s);
  }
}