从超链接而不是从文件夹获取图像来填充我的中继器控件

发布于 2024-07-16 19:49:52 字数 2034 浏览 3 评论 0原文

我不想用项目中文件夹中的图片填充我的中继器,而是想用图像所在位置的链接来填充它,如下所示............

http://www.erate.co.za/imgGrab.aspx ?id=99

我必须对代码进行哪些更改才能让我的代码查看图像的超链接而不是“pics”文件夹?

我的页面

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string sBasePath = System.Web.HttpContext.Current.Request.ServerVariables["APPL_PHYSICAL_PATH"];
            if ( sBasePath.EndsWith("\\"))
                sBasePath = sBasePath.Substring(0,sBasePath.Length-1);

            sBasePath = sBasePath + "\\" + "pics";

            System.Collections.Generic.List<string> oList = new System.Collections.Generic.List<string>();
            foreach (string s in System.IO.Directory.GetFiles(sBasePath, "MyPicture.jpg"))
            {
                //We could do some filtering for example only adding .jpg or something
                oList.Add( System.IO.Path.GetFileName( s ));

            }
            repImages.DataSource = oList;
            repImages.DataBind();
        }

    }

为我的中继器加载我的 ItemDataBound 事件(称为repImages)

 protected void repImages_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem ||
            e.Item.ItemType == ListItemType.Item)
        {
            string sFile = e.Item.DataItem as string;

            //Create the thumblink
            HyperLink hlWhat = e.Item.FindControl("hlWhat") as HyperLink;
            hlWhat.NavigateUrl = ResolveUrl("~/pics/" + sFile  );
            hlWhat.ToolTip = System.IO.Path.GetFileNameWithoutExtension(sFile);
            hlWhat.Attributes["rel"] = "imagebox-bw";

            Image oImg = e.Item.FindControl("imgTheImage") as Image;
            oImg.ImageUrl = ResolveUrl("~/createthumb.ashx?gu=/pics/" + sFile + "&xmax=100&ymax=100" );


        }

    }

Etienne

Instead of populating my Repeater with pictures from a folder in my project i want to polulate it with a link to where the image is like so..............

http://www.erate.co.za/imgGrab.aspx?Id=99

What code changes must i make to let my code look at my hyperlink for the image and not the "pics" folder?

My Page Load

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string sBasePath = System.Web.HttpContext.Current.Request.ServerVariables["APPL_PHYSICAL_PATH"];
            if ( sBasePath.EndsWith("\\"))
                sBasePath = sBasePath.Substring(0,sBasePath.Length-1);

            sBasePath = sBasePath + "\\" + "pics";

            System.Collections.Generic.List<string> oList = new System.Collections.Generic.List<string>();
            foreach (string s in System.IO.Directory.GetFiles(sBasePath, "MyPicture.jpg"))
            {
                //We could do some filtering for example only adding .jpg or something
                oList.Add( System.IO.Path.GetFileName( s ));

            }
            repImages.DataSource = oList;
            repImages.DataBind();
        }

    }

My ItemDataBound event for my Repeater (called repImages)

 protected void repImages_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem ||
            e.Item.ItemType == ListItemType.Item)
        {
            string sFile = e.Item.DataItem as string;

            //Create the thumblink
            HyperLink hlWhat = e.Item.FindControl("hlWhat") as HyperLink;
            hlWhat.NavigateUrl = ResolveUrl("~/pics/" + sFile  );
            hlWhat.ToolTip = System.IO.Path.GetFileNameWithoutExtension(sFile);
            hlWhat.Attributes["rel"] = "imagebox-bw";

            Image oImg = e.Item.FindControl("imgTheImage") as Image;
            oImg.ImageUrl = ResolveUrl("~/createthumb.ashx?gu=/pics/" + sFile + "&xmax=100&ymax=100" );


        }

    }

Etienne

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绝對不後悔。 2024-07-23 19:49:52

如果我理解正确的话,这是一个多方面的问题

: 您希望从 Internet URL 而不是本地文件夹加载图像。 这意味着您必须有某种方式列出远程 URL 上可用的文件。 如果您客观地看待问题,您将意识到从远程位置检索图像和从本地系统检索图像之间的唯一区别在于可以使用 DirectoryInfo 类轻松枚举文件。 这里的 GetFiles 方法返回一个 FileInfo 数组,您可以使用 FileName 属性获取每个文件的实际文件名。 所以基本上,您需要一个映射到文件位置的字符串列表。

b. 仅从远程位置枚举文件取决于许多因素,例如您用于检索该列表的协议。 如果您使用 HTTP,并且我们可以假设您对该位置的服务器有一定的控制权,那么 URL 需要主要支持目录浏览。 然后,您需要解析所提供的目录列表(这是非标准的且特定于服务器的)并将所有可用的图像 URL 作为字符串加载。 然后您可以使用该列表填充您的中继器。 请参阅此讨论了解可能的解决方案。

如果服务器支持 FTP,那么您的工作就会变得更容易,因为您可以使用 WebRequestMethods.Ftp.ListDirectory 或 WebRequestMethods.Ftp.ListDirectory 方法枚举图像。

C。 如果您想为图像创建缩略图,则必须编写代码来下载每个图像,将其保存在临时位置,然后对其执行尺寸操作。

d. 但是,如果 Internet 位置上可用的图像列表是静态的,并且您已经拥有该信息,则可以简单地将该列表加载为字符串列表,并将每个 Image 控件的 src 属性设置为该列表。 然而,缩略图仍然是一个问题,除非您也可以在该 URL 上传缩略图创建器(HTTP 处理程序),以便在本地检索文件进行操作,并自动为您提供缩略图。

If I understand it correctly, this is a multi-faceted problem :

a. You wish to load images from an internet URL instead of a local folder. This means that you have to have some way of listing the files available at the remote URL. If you look at the problem objectively, you will realize that the only difference between retrieving images from a remote location and from your local system is that the files are easily enumerable using the DirectoryInfo class. The GetFiles method here returns an array of FileInfo and you can get the actual filename for each using the FileName property. So basically, you need a list of strings that map to the location of the file.

b. Enumerating the files from a remote location alone depends on many factors such as the protocol you are using to retrieve that list. If you're using HTTP, and we can assume that you have some control over the server at that location, then the URL would need to primarily support directory browsing. You will then need to parse that served directory list (which is non-standard and server specific) and load all the available image URL's as strings. You can then populate your Repeater with that list. See this discussion for a possible solution.

If the server supports FTP, then you work becomes easier because you can enumerate the images using the WebRequestMethods.Ftp.ListDirectory or WebRequestMethods.Ftp.ListDirectory methods.

c. If you want to create thumbnails for the images, you will have to write code to download each image, save it in a temporary location and then perform dimensional manipulations upon it.

d. If however, the list of images available at the internet location is static and you already have that information, you can simply load that list as a list of strings and set each Image control's src property to that list. The thumbnails will however remain a problem, unless you can upload your Thumbnail creator (the HTTP handler) at that URL too, so that files are retrieved locally for manipulation and thumbnails are served to you automatically.

国产ˉ祖宗 2024-07-23 19:49:52

在转发器的模板中包含 标签。 如果您需要以编程方式设置源,请将其设为 runat="server"

In the repeater's template include an <img> tag. If you need to programmatically set the source, make it runat="server".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文