访问该路径被拒绝asp.net

时间:2013-11-29 05:53:48

标签: c# asp.net .net sql-server image

我尝试使用此代码从sql数据库中检索图像并在Image1 asp控件中显示:

cn.Open();
    SqlCommand cm = new SqlCommand("select * from ImageCollection where img_id='" + DropDownList1.SelectedItem.ToString() + "'", cn);
    SqlDataAdapter da = new SqlDataAdapter(cm);
    SqlDataReader dr = cm.ExecuteReader();
    try
    {
        if (dr.Read())
        {

            string image1 = Convert.ToString(DateTime.Now.ToFileTime());
            FileStream fs1 = new FileStream(image1, FileMode.CreateNew, FileAccess.Write);
            byte[] bimage1 = (byte[])dr["passport_photo"];
            fs1.Write(bimage1, 0, bimage1.Length - 1);
            fs1.Flush();
            Image1.ImageUrl = "~/images/"+DropDownList1.SelectedItem.ToString();
        }
        dr.Close();
        cn.Close();
    }
    catch (Exception ex)
    {
        throw ex;
    }

但我最终得到了这个:

  

访问路径'C:\ Program Files(x86)\ Common Files \ Microsoft   共享\ DevServer \ 10.0 \ 130301775684384514'被拒绝。

如何在不改变整个代码的情况下克服这一点。(

1 个答案:

答案 0 :(得分:1)

string base64String = Convert.ToBase64String(bytes, 0, bytes.Length); 
Image1.ImageUrl = "data:image/png;base64," + base64String;