Getting image from hyperlink and not from folder to populate my Repeater Control

别说谁变了你拦得住时间么 提交于 2019-12-25 00:55:18

问题


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


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/728862/getting-image-from-hyperlink-and-not-from-folder-to-populate-my-repeater-control

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!