2

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

2
  • 3
    When dealing with website images use only relative paths and do not navigate outside the root folder. So remove Server.MapPath. Commented Aug 3, 2018 at 9:23
  • Yes genius , that was the problem, @VDWWD thanks man Commented Aug 3, 2018 at 9:27

1 Answer 1

1

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).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.