从超链接而不是从文件夹中获取图像以填充我的Repeater控件 [英] Getting image from hyperlink and not from folder to populate my Repeater Control

查看:100
本文介绍了从超链接而不是从文件夹中获取图像以填充我的Repeater控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不想用我项目中某个文件夹中的图片填充我的Repeater,而是想用一个指向图像的链接来填充它……........

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

我必须更改哪些代码让我的代码查看图像的超链接而不是 pics文件夹?

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

我的页面加载

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();
        }

    }

我的Repeater的MyItemDataBound事件(称为repImages)

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)

Etienne

推荐答案

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

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

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

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。仅从远程位置枚举文件取决于许多因素,例如用于检索该列表的协议。如果您使用的是HTTP,并且我们可以假定您对该位置的服务器有一定的控制权,则该URL首先需要支持目录浏览。然后,您需要解析该提供的目录列表(非标准且特定于服务器),并将所有可用的图像URL加载为字符串。然后,您可以使用该列表填充 Repeater。有关可能的解决方案,请参见此讨论

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.

如果服务器支持FTP,则工作变得更加轻松,因为可以使用WebRequestMethods.Ftp.ListDirectory或WebRequestMethods.Ftp.ListDirectory方法枚举图像。

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。如果要为图像创建缩略图,则必须编写代码以下载每个图像,将其保存在临时位置,然后对其执行尺寸操作。

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。但是,如果互联网上可用的图像列表是静态的,并且您已经有了该信息,则可以简单地将该列表作为字符串列表加载,并将每个Image控件的src属性设置为该列表。但是,缩略图仍然是个问题,除非您也可以在该URL上上传缩略图创建器(HTTP处理程序),这样才能在本地检索文件以进行操作,并且缩略图会自动提供给您。

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.

这篇关于从超链接而不是从文件夹中获取图像以填充我的Repeater控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆