如何列出Web服务器上某些文件夹中的某些特定图像?

时间:2022-03-14 00:29:55

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

让我解释一下:这是此文件夹的路径:> 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

并且图像由user_id创建,例如,user_id = 27的图像是,27_1.jpg,27_2.jpg,27_3.jpg!如何列出和打印以27 _%。jpg开头的图像?我希望你了解我! PS。我完全是ASP.NET(VB)的初学者,请给我详细的信息

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:

我需要创建XML,直到现在我创建了这样的:

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

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)

在数据库中不存在此图像的列表,这就是我必须在文件夹中计算它们的原因。 (这不是我的网站,但我需要在这个网站上创建XMl)

Do you understand me?

你了解我吗?

2 个解决方案

#1


1  

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:

然后,在ASPX页面上,您将拥有一个名为dlImages的数据列表控件,其定义如下:

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

#2


1  

The appropriate method would be to do the following

适当的方法是执行以下操作

  1. Get the listing of files using System.IO.Directory.GetFiles("YourPath", UserId + "_*.jpg")
  2. 使用System.IO.Directory.GetFiles(“YourPath”,UserId +“_ *。jpg”)获取文件列表

  3. Loop through this listing and build your XML or then render it out to the user.
  4. 遍历此列表并构建XML,然后将其呈现给用户。

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

基本上,GetFiles方法接受一个路径,以及一个允许您进行通配符搜索的“过滤器”参数!

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.

编辑:GetFiles操作返回表示完整文件名的字符串列表,然后您可以使用System.IO.Path.GetFileName()方法操作这些值以获取实际文件名。

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.

如果要实际构建文档,可以使用XmlDocument类,也可以使用简单循环和字符串构建器来完成。像下面这样的东西。

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");

#1


1  

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:

然后,在ASPX页面上,您将拥有一个名为dlImages的数据列表控件,其定义如下:

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

#2


1  

The appropriate method would be to do the following

适当的方法是执行以下操作

  1. Get the listing of files using System.IO.Directory.GetFiles("YourPath", UserId + "_*.jpg")
  2. 使用System.IO.Directory.GetFiles(“YourPath”,UserId +“_ *。jpg”)获取文件列表

  3. Loop through this listing and build your XML or then render it out to the user.
  4. 遍历此列表并构建XML,然后将其呈现给用户。

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

基本上,GetFiles方法接受一个路径,以及一个允许您进行通配符搜索的“过滤器”参数!

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.

编辑:GetFiles操作返回表示完整文件名的字符串列表,然后您可以使用System.IO.Path.GetFileName()方法操作这些值以获取实际文件名。

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.

如果要实际构建文档,可以使用XmlDocument类,也可以使用简单循环和字符串构建器来完成。像下面这样的东西。

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");