页面输出数据库中的Byte数据

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string id = Request.QueryString["id"].ToString();
            using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["conn"].ConnectionString))
            {
        using (SqlCommand cmd = new SqlCommand("select * from Market where id="+int.Parse(id)+"", conn))
                {
                    conn.Open();
                    using (SqlDataReader sdr = cmd.ExecuteReader())
                    {
                       sdr.Read();
                       byte[] myByte = (byte[])sdr["Content"];
                       string filetig = sdr["FName"].ToString().Substring(sdr["Fname"].ToString().LastIndexOf('.'));
                       sdr.Close();
                       string contenttype = "application/octet-stream";    // .*
                       if (filetig == ".jpg" || filetig == ".gif" || filetig == ".emp" || filetig == ".JPG" || filetig == ".GIF" || filetig == ".EMP")
                       {
                           contenttype = "image/GIF";
                        }
                       else if(filetig==".doc" || filetig==".txt")
                       {
                           contenttype = "application/msword";
                       }
                       else if (filetig == ".html")
                       {
                           contenttype = "text/html";
                       }
                       Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
                       Response.ContentType = contenttype;
                       Response.Clear();
                       Response.BinaryWrite(myByte);
                       Response.End();
                      }
                }
            }
         }
    }
posted @ 2008-04-09 16:07  Microbar  阅读(384)  评论(0编辑  收藏  举报