Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Although the URL is correct, I can't show image in either Image or ImageButton control. I tested the url by browsing to it in address bar. It works with data uri and not with location.

I'm assigning image to the control in page load. Here's the CS code:

Image1.ImageUrl = Server.MapPath("UserDP\\14.jpeg");

An empty image control is all I see.
Posted

1 solution

Server.MapPath returns the physical path of the file on the server.
For example: C:\inetpub\wwwroot\UserDP\14.jpeg

Unless the client has an image in exactly the same path on their computer, that won't work.
(Hint: It works when you browse to the URL directly because you're testing the site locally. The server and client are the same computer.)

To display an image in a web application, you need to use either an absolute path, a relative path, or an app-relative path. For example, assuming the UserDP folder is in the root of your site, use:
C#
Image1.ImageUrl = "~/UserDP/14.jpeg";
 
Share this answer
 
v2
Comments
Akshay Raut 7-Oct-14 11:57am    
I'm not running this website on IIS. Also I tried with breakpoint, the location is exactly what I want. After the page is displayed, I copy the URL of image by right clicking and check if the path is correct and it actually is. It's nowhere near inetpub. And I tried exact location too, though they were same. What do I do?
Richard Deeming 7-Oct-14 12:00pm    
Your question says this is an ASP.NET application. If that's not the case, then you need to edit your question.

If it is an ASP.NET application, then my answer stands. You CANNOT use the physical path of a file ON THE SERVER as the ImageUrl of an Image control. You must use a publicly accessible URL on your site.

<img src="C:\Some\Path\Here\file.jpg" /> will NOT work.
<img src="/relative/path/here/file.jpg" /> will work.
Dave Kreskowiak 7-Oct-14 12:00pm    
Well, here's the problem. It doesn't matter what YOU want the location to be. It's what the SERVER want's the URL to be.

The solution is correct IF the image is in a folder under your website root directory, such as C:\inetpub\wwwroot\MyWebApp\UserDP\14.jpeg.

The tilde (~) character maps to the URL root of your site, such as "http://MyWebApp/. So, if the path specified is "~/UserDP/14.jpeg", the resulting URL will be "http//MyWebApp/UserDP/14.jpeg".

In YOUR code, since you used Server.MapPath(), the url is not valid since you're specifying a path that is only legal if the browser is running on the server!

Since you don't specify what the path is under your application root folder, this is about the best information you're going to get.
Akshay Raut 7-Oct-14 12:03pm    
Maybe you're right. But that's not the location I see with breakpoint. It does point to the image location and still its not displayed. Don't be confused because I tried actual path too. It didn't work.
Richard Deeming 7-Oct-14 12:09pm    
You don't seem to understand.

The path you're asking the image control to display is the physical path of the file on the server. The fact that you can see that file when you are on the server is irrelevant. Any other computer trying to access your site will not be able to see that file, because you're asking them to load it from their own hard drive.

You MUST provide a URL to the ImageUrl property. The clue is in the name!

C:\Some\Path\Here\file.jpg is NOT a URL. It is a physical file path.

Let me say this again, because you don't seem to be listening:
YOU CANNOT USE THE PHYSICAL PATH OF A FILE ON YOUR SERVER AS AN IMAGE URL!!!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900