Richu R
Richu R

Reputation: 71

Image file is not showing even though the image url is assigned C#

Hi I tried a code to display a saved image from a folder to an asp:image control using image url. I used the following code

<asp:Image ID="Image1" runat="server" Height="71px" Width="141px" />

and code in the cs page as

string theFileName = Path.Combine(Server.MapPath("~/images/Signature"), Filename);
Image1.ImageUrl = theFileName;
Image1.DataBind();

I can access the same image file from the address bar though the url for the image is correct. then what is the problem with this code? when I inspect the webpage then it shows could not load the image. I am here attaching the scree shot for the same

enter image description here

Upvotes: 2

Views: 777

Answers (1)

SandRock
SandRock

Reputation: 5563

Server.MapPath is used to determine the physical path (C:...\img...) of a file in a web project (~/img/...).

When displaying HTML, you should use URL paths ~/img/... / /img/... / img/.... Thus don't use Server.MapPath on HTML tags.

Don't forget that users use a web browser to display pages and pictures. All contents they can access are only available through URLs. They don't have access to your hard drives directly. The web server maps the path in a URL to a resource. Resources can be dynamic (.aspx page) or static (a picture on hard drive).

Upvotes: 1

Related Questions