开发者

Setting Absolute ImageUrl

I have an Image control on my .aspx page and I want to set the ImageUrl to some开发者_StackOverflow社区thing like this:

<asp:Image ID="imgLogo" ImageUrl="C:\ExternalImages\logo.jpg" runat="server" />

But this doesn't work. I read about the ImageUrl property on MSDN and it says that the url could have either be an absolute or relative path. But how can i set the complete path like shown above?


"Absolute" means absolute from the client's perspective, as in http://foo.com/images/logo.jpg or /images/logo.jpg. The difference is either the fully-qualified domain name or the leading "/", which tells the browser to load that path from the root, or domain-level.

"Relative" means no leading slash, and tells the browser to navigate to the path using the current folder as the starting point. So ../images/logo.jpg is a relative path meaning "relative to the current folder, move up one folder, then down into the images folder, then logo.jpg"

The example you've shown instructs the end-user's browser to load that path from their own machine, which in most cases won't work since they don't have that path or file :)


By absolute url, they mean the entire IIS path to the URL (Not your disk directory path). (i.e. http://yourVirtualDirectory/ExternalImages/logo.jpg).

Create a virtual directory on your IIS hosting machine that points to C:\ExternalImages. Use that virtual directory path in your control.


The ImageUrl is not the computer's path but a Url path: http://www.something.com/logo.jpg" or "/logo.jpg"


You could as well get the image from the disk, but you have to add: file:// but will not run in any other machine than were you are developing-


Absolute and relative URL reffers to the relation on the server.

ex: http://mydomain.se/mysite/images/image.gif is an absolute path /images/image.gif is a relative path. Relative in the way that you page is located in the Mysite directory on the server.

The reason for using relative paths is that it makes it easier to move the site around, The reason to use a absolute path is that you can move the "myimage.html" to a subdirectory without breaking the url.


Absolute means Absolute from within your project, relative means relative the current position e.g. ../../Images/logo.jpg. You are best to use absolute for greatest flexibility e.g. ~Images\logo.jpg. The tilde ~ gets replaced automatically with the correct relative path at runtime.


That folder must be inside your virtual folder, so you can do:

<asp:image imageurl = "/ExternalImages/logo.jpg" runat="server">

Another option is to create a page which can read that file, so you could to write:

<asp:image imageurl = "MyImage.aspx?name=logo.jpg" runat="server">

And your MyImage.aspx.cs will look like:

protected void Page_Load(object sender, EventArgs e)
{
    string basePath = @"c:\ExternalFolder";
    string combined = Path.Combine(basePath, Request.QueryString["name"]);
    if(!basePath.Equals(Path.GetPathRoot(combined), 
        StringComparison.InvariantCultureIgnoreCase))
        throw new System.Security.SecurityException();

    using (FileStream image = new FileStream(combined, FileMode.Open))
    {
        int length = (int)image.Length;
        byte[] buffer = new byte[length];
        image.Read(buffer, 0, length);

        Response.BinaryWrite(buffer);
    }
}

But note that code can lead to injection problems, as you can pass a "..\" into name parameter and get access to files outside that folder.

So, place that folder inside your virtual directory.

EDIT: To make clear: My suggestion is to place your ExternalFolder inside your virtual directory. That's make you life easier.

Sometimes isn't possible to move a folder. So I also updated code above to deal with non canonical file names.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜