请教:web中显示图片

lhdjk 2004-12-28 10:40:40
我的IIS 中,设定主目录为:d:\inetpub\wwwroot

在d:\inetpub\wwwroot下有images目录
images目录下又有a1目录
a1目录下存在若干图片

我在insertimg页中用:
string sm=Server.MapPath("/images")+"\\"+Session["NodeName"].ToString()+fileName.ToString();生成一个图片路径写入sql数据库表中(remark)字段
写入后sql库表中实际显示为:d:\inetpub\wwwroot\images\a1\mansmall.JPG

现在我用lookimg页显示库表中的图片:
SqlConnection Conn1=new SqlConnection("Data Source=dbserver;Integrated Security=SSPI;Initial Catalog=usertb;User ID=sa;Password=");
string subndindex="select remark from hhtable where remark<>'';
Conn1.Open();
SqlDataAdapter db_sqladaptor1=new SqlDataAdapter(subndindex,Conn1);
DataSet fecthsub=new DataSet();
DataTable fectht=new DataTable();
fecthsub.Tables.Add(fectht);
fecthsub.Tables[0].TableName="fectht";
db_sqladaptor1.Fill(fecthsub,"fectht");
Image1.ImageUrl="'"+fectht.Rows[0][0].ToString()+"'";
但不成功!!!
鼠标右击图片上的×;显示路径为:
http://localhost/Web1/'d:/inetpub/wwwroot/images/a1/mansmall.JPG'


问题:
该如何写入库表,或者该如何读取并转换,才能正确显示remark字段下存的图片路径呢?
...全文
311 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
阿牛在线 2004-12-29
  • 打赏
  • 举报
回复
这样吧
你用文件操作,网络上有很多例子
你就把他们的文件名取出来
然后加上"..\\images\\a1\\"+filename
存到数据库中
取出来时再Server.mappath就可以了
lhdjk 2004-12-28
  • 打赏
  • 举报
回复
是的是的,我的images不在程序目录下
我前边说,是在:d:\inetpub\wwwroot目录下,即:d:\inetpub\wwwroot\images

我应该如何写才能正常显示图片呢?
tinghuyang 2004-12-28
  • 打赏
  • 举报
回复
up
阿牛在线 2004-12-28
  • 打赏
  • 举报
回复
你的images 不和程序在同一目录下吧
那相对目录应该是../images/a1/mansmall.jpg了
lhdjk 2004-12-28
  • 打赏
  • 举报
回复
我在库表在字段存的已经是:images/a1/mansmall.jpg了
但Image1.ImageUrl=fectht.Rows[0][1].ToString();后,右击mouse,看到的还是:http://localhost/Web1/images/a1/mansmall.JPG
为什么呀??????
lhdjk 2004-12-28
  • 打赏
  • 举报
回复
还是不行呀?我现在存入后,数据库表字段显示的是:images\a1\mansmall.JPG
因为是用vs.net写代码,现在运行后,显示的路径是:http://localhost/Web1/images/a1/mansmall.JPG;显然不对
而实际图片路径是:http://localhost/images/a1/mansmall.jpg

该如何写呀?

coloeme 2004-12-28
  • 打赏
  • 举报
回复
写相对路径,绝对路径移植非常麻烦,很少这样用的
写入数据库的时候 直接写如网站目录里的文件夹
数据读取的时候 根据 Server.MapPath + 相对的路径 来取得 图片连接
baddot 2004-12-28
  • 打赏
  • 举报
回复
数据库里只存储相对地址,象 images\a1\mansmall.JPG 这种形式
输出显示的时候组合生成完整的URL,例如 string imgurl = "http://localhost/" + fectht.Rows[0][0].ToString();

另外,图片最好保存在当前项目的目录下面。
server_me 2004-12-28
  • 打赏
  • 举报
回复
写入后sql库表中实际显示为:d:\inetpub\wwwroot\images\a1\mansmall.JPG

不要写入绝对路径,写相对路径就好啦!

trui 2004-12-28
  • 打赏
  • 举报
回复
byte[] bt = new byte[FileLength]; //图像文件临时存储到byte数组
Stream st = UpFile.InputStream; //建立数据流对象
st.Read(bt,0,FileLength); //读取图象文件数据,bt为数据储存体,0为数据指针位置、FileLnegth为数据长度
// SqlConnection conn = new SqlConnection("server=localhost;uid=;password=;database=");
// string sqlCmd = "insert into images (ImageData, ImageContentType, Imagesfz, ImageSize) VALUES (@Image, @ContentType, @Imagesfz, @ImageSize)";
// SqlCommand myCommand = new SqlCommand(sqlCmd,conn);
// myCommand.Parameters.Add("@Image",SqlDbType.Binary,FileLength).Value = bt;
// myCommand.Parameters.Add("@ContentType",SqlDbType.VarChar,50).Value = UpFile.ContentType;
// myCommand.Parameters.Add("@Imagesfz",SqlDbType.VarChar,500).Value = txtDescription.Text;
// myCommand.Parameters.Add("@ImageSize",SqlDbType.BigInt,8).Value = UpFile.ContentLength;
// conn.Open();
// myCommand.ExecuteNonQuery();
// conn.Close();

SqlConnection conn = new SqlConnection("server=localhost;uid=;password=;database=");
// string imdId = Request.Form["TextBox1.Text"];
// string sqlCmd = "select * from images where Imagesfz=@ImageId";
// SqlCommand myCommand = new SqlCommand(sqlCmd,conn);
// myCommand.Parameters.Add("@ImageId",SqlDbType.NVarChar).Value = imdId;
// conn.Open();
// SqlDataReader sd =myCommand.ExecuteReader();
// Response.ContentType=(string)sd["ImageContentType"];
// Response.BinaryWrite((byte[])sd["ImageData"]);
// Response.End();
// conn.Close();

lhdjk 2004-12-28
  • 打赏
  • 举报
回复
trui的方法不行呀,如果图片有1千张,你总不有让我一张一张的写吧?
究竟该如何写才能读出呀?这问题就这么难吗?
trui 2004-12-28
  • 打赏
  • 举报
回复
我做的图片存储不是把路径存到数据库里,而是直接把图片以流的形式存到数据库里,存到固定路径的也做过,但跟数据库就无关了
存:
UP_FILE.PostedFile.SaveAs(Server.MapPath("\\images\\")+txtDescription.Text+".jpg");//将图片存到了默认路径下c:\interput\wwwroot\images
取:
Image1.ImageUrl="C:/Inetpub/wwwroot/images/"+TextBox1.Text.ToString()+".jpg";

存到数据库的要么,我写的很菜
lhdjk 2004-12-28
  • 打赏
  • 举报
回复
就是把它显示出来,现在连图片都不能显示,更谈不上大小了
trui 2004-12-28
  • 打赏
  • 举报
回复
你所说的正常显示图片是什么意思,不明白你说的,是原始大小还是规定的大小

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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