简体   繁体   English

如何在ASP.NET网站上显示硬盘驱动器上的文件夹列表?

[英]How do you display a list of images, from a folder on hard drive, on ASP.NET website?

I am trying to make a simple photo gallery website. 我正在尝试制作一个简单的照片库网站。 Using ASP.NET and C#. 使用ASP.NET和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. 现在我没有设置服务器,但我只是使用Visual Studio在制作网站项目并运行时开始的开发。

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? 我应该使用ListView控件还是DataView或类似的东西? 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. 首先,您需要将要显示的图像放在Web树下。 Let's assume you have done that and they are in folder called Images. 我们假设您已经完成了这些操作,并且它们位于名为Images的文件夹中。 You can then use a Repeater control to display them by data-binding it like so: 然后,您可以使用Repeater控件通过数据绑定来显示它们,如下所示:

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. 然后它将List绑定到转发器,转发器显示其模板中的每个项目,该模板是使用路径作为ImageUrl的Image控件。 It's quick'n'dirty, but works and should be a good starting point. 这很快,但是有效,应该是一个很好的起点。

You're creating an <img> element with a URL of C:\\Users\\Jordan\\Desktop\\Web Images\\SomeImage.jpg . 您正在创建一个URL为C:\\Users\\Jordan\\Desktop\\Web Images\\SomeImage.jpg<img>元素C:\\Users\\Jordan\\Desktop\\Web Images\\SomeImage.jpg Obviously, that won't work in a web browser. 显然,这不适用于Web浏览器。

You should copy the images to a subfolder of your project, and set the URL to a relative URL, like this: 您应该将图像复制到项目的子文件夹中,并将URL设置为相对URL,如下所示:

img.ImageUrl = "~/Web Images/" + Path.GetFileName(s);

(Assuming that the Web Images folder is a subfolder of the application root) (假设Web Images文件夹是应用程序根目录的子文件夹)

For instance 例如

You need to have a way to specify where your images will be stored in your app. 您需要有一种方法来指定图像在应用中的存储位置。 Therefore you need a web config file with the path in it. 因此,您需要一个包含其中路径的Web配置文件。 Or if you want to be really creative, you can store it in a database.... 或者,如果您想要真正有创意,可以将其存储在数据库中....

In your Web Page 在您的网页中

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Images.aspx.cs" Inherits="ImageViewer" %>


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Viewer Demo</title>
        <link href='styles.css' rel='stylesheet' type='text/css' />
    </head>
    <body>
        <form id="form1" runat="server">
            <div id="outer">
                <h2>Viewer Demo</h2>
                <br />            
                <div style="clear:both;">
                    <h4>Using Viewer with the Repeater Control</h4>
                    <p>Repeater control to display a collection of images.</p>
                </div>
                <div style="clear:both;">
                <asp:Repeater ID="RepeaterImages" runat="server">
                    <ItemTemplate>
                        <asp:Image ID="Image" runat="server" ImageUrl='<%# Container.DataItem %>' />
                    </ItemTemplate>
                </asp:Repeater>
                </div>                
                <br />    
            </div>
        </form>
    </body>
    </html>

In your web.config 在你的web.config中

    <appSettings>
        <add key="FilePath" value="~/images"/>
    </appSettings>

and In your code behind .cs file You really need to filter files, that way if someone( maybe you ;) ) puts erroneous files in it, you won't inadvertently include them... 在你的.cs文件后面的代码你真的需要过滤文件,如果某人(也许你;))将错误的文件放入其中,你不会无意中包含它们......

    string filters = "*.jpg;*.png;*.gif";
    string Path = ConfigurationManager.AppSettings["FilePath"].ToString();

    List<String> images = new List<string>();

    foreach (string filter in filters.Split(';'))
    {
        FileInfo[] fit = new DirectoryInfo(this.Server.MapPath(Path)).GetFiles(filter);
        foreach (FileInfo fi in fit)
        {
            images.Add(String.Format(Path + "/{0}", fi));                 
        }
    }

    RepeaterImages.DataSource = images;
    RepeaterImages.DataBind();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM