html页面
<img src="xxxxx.aspx?state=index_head" />xxxx.aspx的后台代码
protected void Page_Load(object sender, EventArgs e)
{
string state = NicRequest.GetQueryString("state"); //获取传入的state
string url = DB.GetAdLink(state); //查询图片链接地址
Response.Write(url);
}打断点测试传入值state有,查询数据库得到的图片url有,数据路里面的图片都用了绝对路径(如:http://localhost:58002/commentweb/AdLink/999.jpg),粘贴在浏览器上可以看到。但是问题html就没显示图片。如何解决?
<img src="xxxxx.aspx?state=index_head" />xxxx.aspx的后台代码
protected void Page_Load(object sender, EventArgs e)
{
string state = NicRequest.GetQueryString("state"); //获取传入的state
string url = DB.GetAdLink(state); //查询图片链接地址
Response.Write(url);
}打断点测试传入值state有,查询数据库得到的图片url有,数据路里面的图片都用了绝对路径(如:http://localhost:58002/commentweb/AdLink/999.jpg),粘贴在浏览器上可以看到。但是问题html就没显示图片。如何解决?
xxxxx.aspx
private void Page_Load(object sender, System.EventArgs e)
{
string ID = Request.QueryString["id"];
string sql= "select photo from Tb where userid=@id";
SqlConnection myConnection = new SqlConnection(connstr);
SqlCommand myCommand = new SqlCommand(sql, myConnection);
myCommand.AddWithValue("@id",ID )
try
{
myConnection.Open();
SqlDataReader dr= myCommand.ExecuteReader(CommandBehavior.CloseConnection);
if(dr.Read())
{
Response.Clear();
Response.ContentType = "image/gif";
Response.BinaryWrite((byte[])dr["photo"]);
}
myConnection.Close();
}
catch (SqlException ex)
{
}
Response.End();
}
img的url要根据不同的state而不同的呀。不是固定的
url获得的是什么形式?
<img alt="" src="xxxx" runat="server" id="img1" />后台:
img1.Src = url;
那就接取url 相对路径AdLink/999.jpg先要得到当前的URL
可以用string url= Request.RawUrl;
然后接取前面的部分
int index=url.IndexOf("commentweb");
string URL= url.Substring(index+11)+"/AdLink/999.jpg";
Response.Write(url);