你怎么显示图像的列表,从硬盘驱动器上的文件夹,在ASP.NET网站? [英] How do you display a list of images, from a folder on hard drive, on ASP.NET website?

查看:184
本文介绍了你怎么显示图像的列表,从硬盘驱动器上的文件夹,在ASP.NET网站?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个简单的图片库网站。使用ASP.NET和C#。
现在我没有架设服务器,但我只是用发展的一个,当你做一个网站项目并运行它的Visual Studio启动。

我有一个包含未知数量的图像,我的硬盘上的文件夹。我想写一张code的,将通过每幅图像,并将它们添加到默认网页。我已经试过这code,但它不工作。我究竟做错了什么?我应该使用ListView控件或数据视图或类似的东西?我需要在以访问的图像添加一个虚拟目录?如果是这样,我怎么来,这个测试服务器上?

另外,我该如何设置这些图片的位置和定位?
例如,我将如何让这个图片是在一条线上垂直集中在网页上?

 保护无效的Page_Load(对象发件人,EventArgs的发送)
{
    字符串[] = filesindirectory Directory.GetFiles(@C:\\用户\\乔丹\\桌面\\网页图片);
    INT I = 1;
    的foreach(在filesindirectory字符串s)
    {
        图片IMG =新的图像();
        img.ID =形象+ i.ToString();
        img.ImageUrl =秒;
        img.Visible = TRUE;
        Page.Controls.Add(IMG);
        我++;    }}


解决方案

首先,你需要将你想要的网页树下显示图像。让我们假设你已经做到了,他们是在文件夹,名为图像。然后,您可以使用一个Repeater控件以显示这些数据绑定它像这样:

这样的事情...

 < ASP:直放站ID =RepeaterImages=服务器>
    <&ItemTemplate中GT;
        < ASP:图片ID =图像=服务器的ImageUrl ='<%#的Container.DataItem%GT;' />
    < / ItemTemplate中>
< / ASP:直放站>

然后在你的背后code:

 保护无效的Page_Load(对象发件人,EventArgs的发送)
{
    字符串[] = filesindirectory Directory.GetFiles(使用Server.Mappath(〜/图片));
    清单<串GT;照片=新的List<串GT;(filesindirectory.Count());    的foreach(在filesindirectory字符串项)
    {
        images.Add(的String.Format(〜/图片/ {0},System.IO.Path.GetFileName(项目)));
    }    RepeaterImages.DataSource =图像;
    RepeaterImages.DataBind();
}

这基本上创建的图像与他们从目录完整路径的数组。
然后,它创建包含对图像的虚拟路径字符串列表。
然后,它结合了列表的直放站,显示每个项目在它的模板,这是它使用路径作为的ImageUrl Image控件。这是quick'n'dirty,但工程和应该是一个很好的起点。

I am trying to make a simple photo gallery website. Using ASP.NET and C#. Right now I don't have a server set up but I am just using the development one that Visual Studio Starts when you make a website project and run it.

I have a folder on my hard drive that contains an unknown number of images. I want to write a piece of code that will go through each image and add them to the default webpage. I have tried this code but it doesn't work. What am I doing wrong? Should I be using a ListView control or a DataView or something like that? Do I need to add a virtual directory in order to access the images? If so, how do I to that on this test server?

ALSO, how do I set the position and alignment of these pictures? For example, how would I make it so that the pictures are in a line vertically and centered on the webpage?

protected void Page_Load(object sender, EventArgs e)
{
    string[] filesindirectory = Directory.GetFiles(@"C:\Users\Jordan\Desktop\Web Images");
    int i = 1;
    foreach (string s in filesindirectory)
    {
        Image img = new Image();
        img.ID = "image" + i.ToString();
        img.ImageUrl = s;
        img.Visible = true;
        Page.Controls.Add(img);
        i++;

    }

}

解决方案

First you need to place the images you want to display under the web tree. Let's assume you have done that and they are in folder called Images. You can then use a Repeater control to display them by data-binding it like so:

Something like this...

<asp:Repeater ID="RepeaterImages" runat="server">
    <ItemTemplate>
        <asp:Image ID="Image" runat="server" ImageUrl='<%# Container.DataItem %>' />
    </ItemTemplate>
</asp:Repeater>

And then in your code behind:

protected void Page_Load(object sender, EventArgs e)
{
    string[] filesindirectory = Directory.GetFiles(Server.MapPath("~/Images"));
    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();
}

This basically creates an array of images with their full path from the directory. It then creates a List of strings that contain the virtual path to the image. It then binds that List to the repeater, which displays each item in it's template, which is an Image control which uses the path as the ImageUrl. It's quick'n'dirty, but works and should be a good starting point.

这篇关于你怎么显示图像的列表,从硬盘驱动器上的文件夹,在ASP.NET网站?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆