新手~!image控件显示本地图片问题

沐浴-vip 2009-12-08 08:28:01
以下为起始页文档Default.aspx;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

public partial class _Default : System.Web.UI.Page
{

protected void Page_Load(object sender, EventArgs e)
{

}
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (ListBox1.Items.Count > 0)
{
TextBox1.Text = ListBox1.SelectedItem.Text;

Image1.ImageUrl = "Show.aspx?uid=" + ListBox1.SelectedValue;
}

}
rotected void Button1_Click(object sender, EventArgs e)
{
Response.Write("<script>alert('成功~!')</script>");
Image1.ImageUrl="image.aspx?uid="+FileUpload1.PostedFile.FileName;//image控件url指定为image.aspx }
protected void Button3_Click(object sender, EventArgs e)
{
if (ListBox1.Items.Count > 0)
{
string con = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
SqlConnection sqlcon = new SqlConnection(con);
string sqlstr = "delete from photolist where id=@id";
SqlCommand cmd = new SqlCommand(sqlstr, sqlcon);
TextBox1.Text = ListBox1.SelectedItem.Text;
cmd.Parameters.Add("@id", SqlDbType.Int).Value = ListBox1.SelectedValue;
sqlcon.Open();
if (cmd.ExecuteNonQuery() > 0)
{
Response.Write("<script>alert('删除成功~!')</script>");
ListBox1.DataBind();
ListBox1.SelectedIndex = ListBox1.Items.Count - 1;
ListBox1_SelectedIndexChanged(sender, e);
}
if (ListBox1.Items.Count > 0)
TextBox1.Text = ListBox1.SelectedItem.Text;
else
TextBox1.Text = null;
}
else {
Response.Write("<script>alert('document is empty~!')</script>");
}

}

protected void Button4_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
string con = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;

string SelSQL = "Insert photolist Values(@name,@Photo)";

SqlConnection mycon = new SqlConnection(con);

SqlCommand cmd = new SqlCommand(SelSQL, mycon);

cmd.Parameters.Add("@name", SqlDbType.VarChar);
cmd.Parameters["@name"].Value = TextBox1.Text;


#region //构造一个Byte 数组,用以接收上传图片的流数据

byte[] uploadimage = null;


int len = FileUpload1.PostedFile.ContentLength;

uploadimage = new byte[len];

Stream mystream = FileUpload1.PostedFile.InputStream;

mystream.Read(uploadimage, 0, len);

#endregion

cmd.Parameters.Add("@Photo", SqlDbType.VarBinary);
cmd.Parameters["@Photo"].Value = uploadimage;

mycon.Open();

try
{
cmd.ExecuteNonQuery();

Response.Write("<script>alert('图片上传成功!')</script>");
ListBox1.DataBind();
ListBox1.SelectedIndex = ListBox1.Items.Count - 1;
ListBox1_SelectedIndexChanged(sender, e);

}
catch (SqlException ex)
{

throw (ex);
}

}
else
{
Response.Write("<script>alert('请指定加载图片的正确路径~!')</script>");
}

}
}
image.aspx 文档 本地读取并显示图片,vs2008调试的时候这里的图片可以显示,可是发布到IIS7上的时候这里就无法显示图片了,图片显示为"叉叉",路径显示却是对的"http://localhost:49478/Test1106/image.aspx?uid=C:\Users\Public\Pictures\Sample Pictures\Koala.jpg"
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO ;

public partial class image : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string str =Request.QueryString["uid"].ToString ();//获取要显示的图片路径
if(str !=null)
{

#region
FileStream fs = new FileStream(str, FileMode.Open, FileAccess.Read);
BinaryReader r = new BinaryReader(fs);
int count = (int)fs.Length;
byte[] b=new byte[count];
b = r.ReadBytes(count);//读取字节数组;
#endregion

if (count> 0)
{

Response.Clear();
Response.ContentType = "image/jpeg";
//Response.WriteFile(str);
Response.BinaryWrite(b);//b为图片内容的字节数组,在该页显示该图片;
Response.End();
}
}

}
}
show.aspx 文档using System从数据库读取并显示图片,无论是在vs2008调试还是IIS7显示的图片都是“正常”的;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Show : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["uid"] == null)
{
Response.Redirect("Default.aspx");
return;
}



Response.Clear();

string con = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;

string SelSQL = "SELECT Photo FROM photolist WHERE id=@id";

SqlConnection mycon = new SqlConnection(con);

SqlCommand cmd = new SqlCommand(SelSQL,mycon);

cmd.Parameters.Add("@id", SqlDbType.Int);
cmd.Parameters["@id"].Value = int.Parse(Request.QueryString["uid"]);

mycon.Open();

try
{
SqlDataReader read = cmd.ExecuteReader(CommandBehavior.CloseConnection);

if (read.Read())
{
Response.ContentType = "image/gif";

Response.BinaryWrite((byte[])read["Photo"]);

Response.End();
mycon.Close();

}


}
catch (Exception ex)
{

throw(ex);
}


}
}
image.aspx 文档是从本地读取并显示图片,vs2008调试的时候image.aspx这里的图片可以显示,可是发布到IIS7上的时候image.aspx 页的图片就无法显示了,图片显示为"叉叉",路径却是对的"http://localhost:49478/Test1106/image.aspx?uid=C:\Users\Public\Pictures\Sample Pictures\Koala.jpg

问题出在哪???请高手指教本人用的是windows7 IIS7,vs2008调试的

...全文
948 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
sh_suyuelin 2009-12-09
  • 打赏
  • 举报
回复
太长了。。帮顶
SK_Aqi 2009-12-09
  • 打赏
  • 举报
回复
发布后肯定不是localhost:什么什么的了。。。
应该是http://www.xxxxx.com/image/.....
Image1.ImageUrl = Page.ResolveClientUrl("~") + "image.aspx?uid=" + FileUpload1.PostedFile.FileName;
YnSky 2009-12-09
  • 打赏
  • 举报
回复
最好把图片放在项目中.不用的时候可以用代码删除也行.定时清空文件夹也行.
yanfei_519 2009-12-09
  • 打赏
  • 举报
回复
代码那么多,看了头晕。。。
沐浴-vip 2009-12-09
  • 打赏
  • 举报
回复
注:C:\Users\xxx(用户名)\下的文件无法读取
沐浴-vip 2009-12-09
  • 打赏
  • 举报
回复
image.aspx 文档 本地读取并显示图片,vs2008调试的时候这里的图片可以显示,可是发布到IIS7上的时候这里就无法显示图片了,图片显示为"叉叉",路径显示却是对的"http://localhost:49478/Test1106/image.aspx?uid=C:\Users\Public\Pictures\Sample Pictures\Koala.jpg"
原因已知,在IIS 服务中运行的网站程序无法取到计算机C:\Users\~下文件,换成C:\*.JPG问题就不存在了;且C:\Users\Public\Pictures\Sample Pictures\Koala.jpg是指服务器的路径,而不是客服端电脑的路径
悔说话的哑巴 2009-12-08
  • 打赏
  • 举报
回复
单步调试一下看看
wuyq11 2009-12-08
  • 打赏
  • 举报
回复
Image1.ImageUrl="image.aspx?uid=1";
http://localhost:49478/Test1106/Koala.jpg
保存图片到数据库,在image.aspx输出
或路径到数据库直接连接路径
happy664618843 2009-12-08
  • 打赏
  • 举报
回复
路径一般都是文件夹加上文件名 看文件夹是不是在中同一个目录下如果不在用~/ 单步调试下看路径是否正确
Joetao 2009-12-08
  • 打赏
  • 举报
回复
单步调试下,看看路径对不对!
沐浴-vip 2009-12-08
  • 打赏
  • 举报
回复
不知道为什么项目发布到IIS7的时候,image.aspx页(本地读取的)的图片就显示不出来了,但是show.aspx页内图片显示正常(从数据库读取的图片)注:本人用的windows 7
沐浴-vip 2009-12-08
  • 打赏
  • 举报
回复
在vs2008里调试的时候,image.aspx页的图片都可以显示的,图片是在本地的,就是为了把本地的图片存储在内存里,显示在浏览器上 路径是Default.aspx内的FileUpload控件指定的文件路径
还有Default.aspx(起始页),image.aspx,show.aspx 在同一个项目里
limit_clear 2009-12-08
  • 打赏
  • 举报
回复
把图片复制到解决方案里去就OK
cangyuqihun 2009-12-08
  • 打赏
  • 举报
回复
Image1.ImageUrl="image.aspx?uid="+FileUpload1.PostedFile.FileName;
这句改成
Image1.ImageUrl="~/image.aspx?uid="+FileUpload1.PostedFile.FileName;
liuyjcel 2009-12-08
  • 打赏
  • 举报
回复
你把图片包含到项目中试试.

62,254

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧