图片不显示
我在网站上显示图像列表时遇到问题。
使用这个问题如何在 ASP.NET 网站上显示硬盘上文件夹中的图像列表?
我几乎只是复制了答案,因为这就是我想要做的。但是,我最终一无所获。 我已经检查了浏览器中的页面源,并且图像 url 在
IE 中看起来是正确的。
<a id="MainContent_RepeaterImages_Image_78"><img src="C:\Users\k\Galleries\Photos%20from%20the%20Final%20Conference\IMG_5962.jpg" alt="" />
代码背后:
string[] filesindirectory = Directory.GetFiles(Server.MapPath("Galleries") + "\\Photos from the Final Conference");
List<String> images = new List<string>(filesindirectory.Count());
foreach (string item in filesindirectory)
{
images.Add(String.Format("~/Images/{0}", System.IO.Path.GetFileName(item)));
}
RepeaterImages.DataSource = images;
RepeaterImages.DataBind();
和asp:
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<div id="test">
<asp:Repeater ID="RepeaterImages" runat="server">
<ItemTemplate>
<asp:HyperLink ID="Image" runat="server" ImageUrl='<%# Container.DataItem %>' />
</ItemTemplate>
</asp:Repeater>
</div>
对我所缺少的有什么想法吗?
I'm having trouble showing a list of images on a site.
using this question How do you display a list of images, from a folder on hard drive, on ASP.NET website?
ive pretty much just copied the answer as it is what i want to do. But, i end up with a bunch of nothing.
i've checked the page source in ´the browser and the image urls look correct
IE.
<a id="MainContent_RepeaterImages_Image_78"><img src="C:\Users\k\Galleries\Photos%20from%20the%20Final%20Conference\IMG_5962.jpg" alt="" />
code behind:
string[] filesindirectory = Directory.GetFiles(Server.MapPath("Galleries") + "\\Photos from the Final Conference");
List<String> images = new List<string>(filesindirectory.Count());
foreach (string item in filesindirectory)
{
images.Add(String.Format("~/Images/{0}", System.IO.Path.GetFileName(item)));
}
RepeaterImages.DataSource = images;
RepeaterImages.DataBind();
and the asp:
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<div id="test">
<asp:Repeater ID="RepeaterImages" runat="server">
<ItemTemplate>
<asp:HyperLink ID="Image" runat="server" ImageUrl='<%# Container.DataItem %>' />
</ItemTemplate>
</asp:Repeater>
</div>
any ideas on what i'm missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您将需要使用存储在应用程序的根目录(即该 Web 应用程序的虚拟目录(根文件夹))内或根目录下的文件的相对路径。
使用完整的物理文件名(据我所知)仅在网页文件本身已使用
file:///
协议(并且“url”可能也需要遵守)。我认为我从来没有遇到过这样做的理由或需要 - 因为相对路径无论哪种方式都可以工作。另外,代码似乎是矛盾的:你有一个原始的 HTML 元素 (
img
),它是用一个可能是硬编码的路径值定义的(假设你无法以自然的方式更新非服务器端控件)从代码中就像使用服务器端控件一样 - 除非我可能对它如何从转发器呈现您的HyperLink
感到困惑。)您显示的生成路径的代码会在它们前面加上前缀〜/图像
,但这没有出现在前面提到的路径中 - 您确定这是所显示代码结果的准确表示吗?您通常需要映射路径才能获得此类输出,并且不太可能忽略指定路径的一部分。You will need to use relative paths to files stored within, or under the root of the application (that is, that web application's virtual directory (root folder)).
Using full, physical file names will only ever work (to my knowledge) in cases where the web-page file itself has been opened locally using the
file:///
protocol (and the 'urls' may need to comply, too). And I don't think I've ever come across a reason or need to do so - because relative paths will work either way.Also, the code seems contradictory: you have a primitive HTML element (
img
) defined with a presumably hard-coded path value (given that you can't update non-server-side controls in the natural way from code as you would with server-side controls - unless maybe I'm getting confused with how it is rendering yourHyperLink
from the repeater.) The code you show which generates paths prefixes them with~/images
, but this doesn't appear in the previously mentioned path - are you sure this is an exact representation of the result of the shown code? You would generally need to map the path to get such an output, and it wouldn't be likely to dismiss part of the specified path.已接受答案的第一句话是:
除非图像位于 Web 应用程序文件夹内的文件夹中,否则它将无法工作。
请注意,第二个答案也指出了这一点。
如果您确实想使用物理路径,请使用 file:/// 方法,如失望先生所说(请注意,它需要是本地路径(对于客户端),但我不能 100% 确定这是否有效在所有浏览器中)。
The first sentence of the accepted answer says:
It won't work unless the images are in a folder within your web application's folder.
Note that the 2nd answer notes this too.
If you really want to use physical paths, then use the file:/// method as Mr. Disappointment notes (be aware it needs to be a local path (for the client) though and i'm not 100% sure if this works in all browsers).
可能您的路径引用不正确,请尝试使用
Server.MapPath()
请查看有关 ASP.NET Web 项目路径
Probably your path may be referencing incorrectly, try to use
Server.MapPath()
Please have a look at this documentation about ASP.NET Web Project Paths