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

后端 未结 3 863
粉色の甜心
粉色の甜心 2020-12-08 22:42

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

3条回答
  •  抹茶落季
    2020-12-08 23:05

    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. 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" %>
    
    
        
        
        
            Viewer Demo
            
        
        
            

    Viewer Demo


    Using Viewer with the Repeater Control

    Repeater control to display a collection of images.


    In your web.config

        
            
        
    

    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...

        string filters = "*.jpg;*.png;*.gif";
        string Path = ConfigurationManager.AppSettings["FilePath"].ToString();
    
        List images = new List();
    
        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();
    

提交回复
热议问题