简体   繁体   中英

How to list some specific images in some folder on web server?

Let me explain: this is path to this folder: > www.my_site.com/images

And images are created by user_id , and for example, images of user_id = 27 are, 27_1.jpg , 27_2.jpg , 27_3.jpg ! How to list and print images which start with 27_%.jpg ? I hope You have understood me! PS. I am totally beginmer in ASP.NET (VB) and please give me detailed information

Here starts my loop

while dbread.Read()

'and then id user_id
dbread('user_id')

NEXT???


I nedd to create XML, till now I created like this:

act.WriteLine("") act.WriteLine(" http://www.my_site.com/images/ "&dbread("user_id")&"_1.jpg") act.WriteLine("")

But this is not answer because I need to create this nodes how many images of this user exist?

In database doesn't exist list of this images so that is reason why I must count them in folder. (this is not my site exacly, but I need to create XMl on this site)

Do you understand me?

The best way is to just loop through all the files in the directory.

While dbRead.Read
  dim sUserId as String= dbread('user_id')
  For Each sFile As String In IO.Directory.GetFiles("C:\")
    if sFile.StartsWith (sUserId) Then
        'Do something.
    End If
  Next
Loop

However, to actually show the images, you're best bet could be to create a datatable of these images, and then use a datalist or repeater control to display them.

Dim dtImages as new DataTable
dtImages.Columns.Add("Filename")
If dbRead.Read
  dim sUserId as String= dbread('user_id')
  For Each sFile As String In IO.Directory.GetFiles("C:\")
    if sFile.StartsWith (sUserId) Then
        Dim drImage as DataRow = dtImages.NewRow
        drImage("Filename") = sFile
        dtImages.Rows.add(drImage)
    End If
  Next
End If
dlImages.DataSource = dtImages
dlImages.DataBind

Then, on your ASPX page, you would have a datalist control called dlImages defined like:

    <asp:datalist id="dlImages" RepeatDirection="Horizontal" runat="server" RepeatLayout="Flow" Height="100%">
        <ItemTemplate>
            <asp:Image ID="Image1" Runat=server ImageUrl='<%# Server.MapPath("photos") & Container.DataItem("FileName") %>'>
            </asp:Image>
        </ItemTemplate>
    </asp:datalist>

The appropriate method would be to do the following

  1. Get the listing of files using System.IO.Directory.GetFiles("YourPath", UserId + "_*.jpg")
  2. Loop through this listing and build your XML or then render it out to the user.

Basically the GetFiles method accepts a path, and a "filter" parameter which allows you to do a wildcard search!

EDIT: The GetFiles operation returns a listing of strings that represent the full file name, you can then manipulate those values using the System.IO.Path.GetFileName() method to get the actual file name.

You can use the XmlDocument class if you want to actually build the document, or you could do it with a simple loop and a string builder. Something like the following.

StringBuilder oBuilder = new StringBuilder();
oBuilder.Append("<root>");
string[] ofiles = Directory.GetFiles("YourPath", "yourMask");
foreach(string currentString in oFiles)
{
    oBuilder.AppendLine("<file>http://yourpath/" + Path.GetFileName(currentString) + "</file>");
}
oBuilder.Append("</root");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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