GridView怎样才能显示出照片?
照片在磁盘上的存储位置是D:\Others\Items\image\P0000001.jpg,在SqlServer2005中相应表中存储为P0000001.jpg(对应的...
照片在磁盘上的存储位置是D:\Others\Items\image\ P0000001.jpg,在Sql Server 2005中相应表中存储为P0000001.jpg(对应的字段名为Photo),为什么在C#中使用下列两种方法都显示不出照片,而只能显示出一个红色的小叉叉(利用Gridview绑定):
1.<asp:ImageField HeaderText="照片" DataImageUrlField="Photo" DataImageUrlFormatString="~/image/{0}.jpg">
</asp:ImageField>
2.<asp:TemplateField HeaderText="Photo" SortExpression="Photo">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Photo") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("Photo","../../image/{0}") %>' />
</ItemTemplate>
</asp:TemplateField>
问题在哪里?该怎样修改?敬请高手指点!谢谢! 展开
1.<asp:ImageField HeaderText="照片" DataImageUrlField="Photo" DataImageUrlFormatString="~/image/{0}.jpg">
</asp:ImageField>
2.<asp:TemplateField HeaderText="Photo" SortExpression="Photo">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Photo") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("Photo","../../image/{0}") %>' />
</ItemTemplate>
</asp:TemplateField>
问题在哪里?该怎样修改?敬请高手指点!谢谢! 展开
展开全部
ImageUrl必须指向服务器端的一个aspx或者ashx, 然后在这个aspx或ashx中用Response把对应的文件的内容发送到客户端
public static void SendFileToClient(HttpResponse Response, string AsFileName, string FileFullPath, string ContentType)
{
if (String.IsNullOrEmpty(ContentType))
{
ContentType = CommonTools.GetMimeType(AsFileName);
}
Response.ContentType = ContentType;
if (!string.IsNullOrEmpty(AsFileName))
Response.AddHeader("Content-Disposition", "attachment; filename=\"" + HttpUtility.UrlEncode(AsFileName) + "\"");
else
Response.AddHeader("Content-Disposition", "attachment; filename=TMPFile.tmp");
if (!System.IO.File.Exists(FileFullPath))
return;
System.IO.FileStream fs = new System.IO.FileStream(FileFullPath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read);
try
{
int fileLength = (int)fs.Length;
int startIndex = 0;
byte[] data = new byte[BufferSize];
while (startIndex + BufferSize < fileLength)
{
fs.Read(data, 0, data.Length);
Response.BinaryWrite(data);
startIndex += data.Length;
}
byte[] lastChunk = new byte[fileLength - startIndex];
fs.Read(lastChunk, 0, fileLength - startIndex);
Response.BinaryWrite(lastChunk);
}
catch (Exception)
{
}
finally
{
fs.Close();
fs.Dispose();
}
}
public static void SendFileToClient(HttpResponse Response, string AsFileName, string FileFullPath, string ContentType)
{
if (String.IsNullOrEmpty(ContentType))
{
ContentType = CommonTools.GetMimeType(AsFileName);
}
Response.ContentType = ContentType;
if (!string.IsNullOrEmpty(AsFileName))
Response.AddHeader("Content-Disposition", "attachment; filename=\"" + HttpUtility.UrlEncode(AsFileName) + "\"");
else
Response.AddHeader("Content-Disposition", "attachment; filename=TMPFile.tmp");
if (!System.IO.File.Exists(FileFullPath))
return;
System.IO.FileStream fs = new System.IO.FileStream(FileFullPath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read);
try
{
int fileLength = (int)fs.Length;
int startIndex = 0;
byte[] data = new byte[BufferSize];
while (startIndex + BufferSize < fileLength)
{
fs.Read(data, 0, data.Length);
Response.BinaryWrite(data);
startIndex += data.Length;
}
byte[] lastChunk = new byte[fileLength - startIndex];
fs.Read(lastChunk, 0, fileLength - startIndex);
Response.BinaryWrite(lastChunk);
}
catch (Exception)
{
}
finally
{
fs.Close();
fs.Dispose();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
显示照片直接img标记就好了,数据绑定的时候直接把图片url绑定到img标记上!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
啊
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
a
追问
谢谢!我回去再试试!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询