大家好,我是菜鸟,请教一个问题,我在SQL2005数据库中存储了图片的路径,对应的字段为imagePath,通过传递ID的值来调用图片,将其显示在页面上,一条路径下对应的图片有很多张,即路径中我没有存储图片名字,如存储路径为D:\My pictures\,路径下有image1.jpg;image2.jpg等多张图片,请问我要怎样才能就将图片显示出来呢,显示图片用哪个控件好呢,我打算用FromView,不知道是否可取
解决方案 »
- GridView中的input(text)数据绑定与数据回传,头大了!!
- 无限级分类,迭代
- 点击网页后退按钮返回上页时为什么不执行 Private Sub Page_Load中的语句
- 深圳的朋友 谁知道“亚思博”这个公司怎么样呀???
- 版主等高手帮忙啊!asp.net(c#)中如何调用java类???十万火急
- ComponentArt Menu 客户端代码删除菜单项问题!
- global文件竟然没有,为何???---waiting!!!
- 如何在打开页面时就编辑Datalist的第一行记录?
- mvc一个视图三块内容怎么设计
- 高分问一个比较简单的问题,高手进来看看
- 页面打印
- silverlight数据源问题
绑定图片显示可以用repeater,里面套个img<img src='%# Eval("ImagePath")%> ' />
<img src="My pictures/<%#Eval("imagePath").ToString()%>" />
这个名称可以在你刚才使用IO读取文件夹时 读取并保存下来.
引入命名空间using System.Text.RegularExpressions和using System.IO;
DirectoryInfo di = new DirectoryInfo(Server.MapPath("~\\res\\"));
FileInfo[] fi = di.GetFiles();
for (int i = 0; i < fi.Length; i++)
{
if (Regex.IsMatch(fi[i].Name.ToLower(), @".(bmp|png|jpg|jpeg|gif)"))
{
Image img = new Image();
img.ImageUrl = fi[i].FullName;
Form.Controls.Add(img);
}
}
<asp:FormView ID="FormView3" runat="server">
<ItemTemplate>
<img alt="" src='<%#Eval("hPath") +"1.jpg" %>' style="width:90px; height:90px"/>
<img alt="" src='<%#Eval("hPath") +"2.jpg" %>' style="width:90px; height:90px"/>
</ItemTemplate>
</asp:FormView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString = "Data Source=.\SQLEXPRESS;Initial Catalog=db1;Integrated Security=True"
SelectCommand="SELECT * FROM housesPics WHERE (ID = @ID)" >
<SelectParameters>
<asp:QueryStringParameter Name="hID" QueryStringField="hID" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
后台要写代码吗?要怎样写啊
res是网站更目录下的文件夹,你可以修改为你自己的名称
他这写的DirectoryInfo di = new DirectoryInfo(Server.MapPath("~\\res\\"));
res 他这是项目下的绝对路径,就是res文件夹下的内容
你可以读取你数据库中的字段
改下就行
<img alt="" runat="server" src='<%#Eval("Path")%>' style="width:220px; height:220px"/>
<asp:FormView ID="FormView3" runat="server" DataSourceID="SqlDataSource1">
<ItemTemplate>
<asp:Repeater ID="Repeater1" runat="server" DataSource='<%# GetPics(Eval("hPath") as string) %>'>
<ItemTemplate>
<img alt="" src='<%# Container.DataItem %>' style="width:90px; height:90px"/>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:FormView><asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString = "Data Source=.\SQLEXPRESS;Initial Catalog=db1;Integrated Security=True"
SelectCommand="SELECT * FROM housesPics WHERE (ID = @hID)" >
<SelectParameters>
<asp:QueryStringParameter Name="hID" QueryStringField="hID" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource> <script runat="server">
public ArrayList GetPics(string path)
{
if (string.IsNullOrEmpty(path))
return null;
path = ResolveUrl(path);
if (!path.EndsWith("/")) path = path + "/"; ArrayList files = new ArrayList();
System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(Server.MapPath(path));
System.IO.FileInfo[] fi = di.GetFiles();
for (int i = 0; i < fi.Length; i++)
{
if (Regex.IsMatch(fi[i].Name.ToLower(), @".(bmp|png|jpg|jpeg|gif)"))
{
files.Add(path + fi[i].Name);
}
}
return files;
}
</script>
if (!path.EndsWith("/")) path = path + "/";
因为我存储的路径是D:\My pictures\地区图片\,路径下有image1.jpg;image2.jpg等多张图片,图片名称和路径中包含中文没问题吧
图片保存的目录是D:\My Documents\MyPictures\,下面有很多图片,image1.jpg,image2.jpg;
这么说来,你的图片根本不在网站下面,你想用户怎么能够访问到你的图片呢?为今之计,你有两条出路:
其一,把图片目录改到你的网站下面,当然这样你可能要重写图片保存部分的程序,数据库里路径也要重写。
其二,在IIS中另建一个虚拟目录,指向D:\My Documents\MyPictures,访问图片时通过这个虚拟目录访问
<img src="My pictures/<%# Eval("imagePath") %>" />这样子就可以了吧!
public ArrayList GetPics(string path)
{
if (string.IsNullOrEmpty(path))
return null; path = Regex.Replace(path, ".*(地区图片.*)", @"~\$1");
path = ResolveUrl(path);
if (!path.EndsWith("/")) path = path + "/"; ArrayList files = new ArrayList();
System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(Server.MapPath(path));
System.IO.FileInfo[] fi = di.GetFiles();
for (int i = 0; i < fi.Length; i++)
{
if (Regex.IsMatch(fi[i].Name.ToLower(), @".(bmp|png|jpg|jpeg|gif)"))
{
files.Add(path + fi[i].Name);
}
}
return files;
}另外,楼主确实应该考虑在数据库里保存相对路径,不要用绝对路径,并且最好把图片的文件名也带上,如果是多个文件用逗号隔开。
<asp:FormView ID="FormView3" runat="server" DataSourceID="SqlDataSource1">
<ItemTemplate>
<asp:Repeater ID="Repeater1" runat="server" DataSource='<%# GetPics(Eval("hPath") as string) %>'>
<ItemTemplate>
<img alt="" src='<%# Container.DataItem %>' style="width:90px; height:90px"/>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:FormView><asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString = "Data Source=.\SQLEXPRESS;Initial Catalog=db1;Integrated Security=True"
SelectCommand="SELECT * FROM housesPics WHERE (ID = @hID)" >
<SelectParameters>
<asp:QueryStringParameter Name="hID" QueryStringField="hID" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource> <asp:FormView ID="FormView4" runat="server" DataSourceID="SqlDataSource2">
<ItemTemplate>
<asp:Repeater ID="Repeater1" runat="server" DataSource='<%# GetPics(Eval("jPath") as string) %>'>
<ItemTemplate>
<img alt="" src='<%# Container.DataItem %>' style="width:90px; height:90px"/>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:FormView><asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString = "Data Source=.\SQLEXPRESS;Initial Catalog=db1;Integrated Security=True"
SelectCommand="SELECT * FROM housesPics WHERE (ID = @hID)" >
<SelectParameters>
<asp:QueryStringParameter Name="hID" QueryStringField="hID" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
再共享GetPics(string path)函数,这样应该是没问题啊,为什么会出错呢?系统提示未能映射路径。
因为我这方面还是新手,所以问得比较详细,谢谢大家的包容,这里高手很多,我很乐意向大家学习
//导入命名空间
using System.IO;
using System.Text; //用StringBuilder需要导入
using System.Drawing;//导入图片操作的命名空间
StringBuilder builder = new StringBuilder();
//上传按钮
protected void btnUpLoad_Click(object sender, EventArgs e)
{
HttpFileCollection files = HttpContext.Current.Request.Files;
if (files.Count <= 0)
{
return;
} for (int i = 0; i <files.Count; i++)
{
if (!UpFile(files[i], i))
{
}
} Response.Write("要保存到数据库的字符串:" + builder.ToString());
string[] str;
str = builder.ToString().Split('@');
for (int i = 0; i < str.Length; i++)
{
Response.Write("截取后的字符串:"+str[i].ToString() +"<br/>");
} } //上传图片
private bool UpFile(HttpPostedFile file, int index)
{
if (!string.IsNullOrEmpty(file.FileName))
{
try
{
//逻辑:从上传的文件的文件流中获得到文件信息,在内存中重构这张图片
//如果图片重构成功则是一张正常的图片,重构失败就不是图片文件
//这个操作会导致异常 一定要放到try中去做
Bitmap a = new Bitmap(file.InputStream);
string fileName = DateTime.Now.ToString("yyyy-MM-dd-hh-mm-ss-fffff") + "-product" + ".jpg";
builder.Append(fileName);
builder.Append("@");
string path = Server.MapPath("~/Upload") + @"\" + fileName;
//file.SaveAs(path);
Response.Write(string.Format("第{0}行{1} 上传成功!<br/>",index+1,file.FileName));
}
catch
{
Response.Write(string.Format("第{0}行{1} 上传的文件不合法!<br/>", index + 1, file.FileName));
return false;
} return true;
}
else
{
Response.Write(string.Format("第{0}行{1} 文件路径不能为空!<br/>", index + 1, file.FileName));
return false;
}
}
[/code]建议先从数据库中取出来,然后再进行字符串截取,再进行重绑。
[code=C#:图片读取]
Image showImage = e.Row.FindControl("showImage") as Image;
string[] str;
str = showImage.ImageUrl.Split('@');
for (int i = 0; i < str.Length; i++)
{
str[i].ToString();
}
showImage.ImageUrl = str[0].ToString();
//MessageBox.Show(this,showImage.ImageUrl);
[/code]
2,读取的时候,你的图片必须在网站目录中,假设在image目录中,那么在image控件中imageUrl路径为~/image/<%#Eval("你的数据字段名")%>.这样就好了.
3.你在一个FM中写两个image控件无非就是在<%#Eval("你的数据字段名")%>中该一下你的数据字段名,只要你的FM已经绑定了数据,就没有问题.用image服务器控件,而不是img静态标签.