Reputation: 17
In the below code i have a image i have pass image url from codebehind but it is not displaying the image.
public string strName = "SV";
public string strFolder = "Documents";
string Imgdocname = SearchDoc.DocumentName;
string fileExt = System.IO.Path.GetExtension(Imgdocname);
fileExt = fileExt.ToLower();
if (fileExt == ".jpeg" || fileExt == ".jpg")
{
docimg.ImageUrl = "C:\\Search\\" + strName + "\\" + strFolder + "\\" + Imgdocname; //not working
docimg.Visible = true;
} else
{docimg.ImageUrl = "C:\\Search\\doc\\Documents\\image.jpeg"; //not working
}
<div ID="imgContainer" runat="server" style="width: 700px; height: 300px; overflow:auto; border: solid 1px black;
padding: 10px; margin-bottom: 5px;" >
<asp:Image ID="docimg" runat="server" />
</div>
Upvotes: 0
Views: 3334
Reputation: 22501
As opposed to a client application, images in a web application are not included into the page directly. Instead, the page contains a link to the image resource that the browser requests after retrieving the HTML of the page. Therefore you need to provide a URL that the client can retrieve. In your sample you set the file path on the server as the image URL. This will not work as the client cannot access the file path on the server directly.
Instead you need to place the images in a folder that is accessible through the web server (e.g. IIS) so that you are able to set a URL like /img/picture.jpg
that the client can use to retrieve the image. The easiest way to achieve this is to place the images in a folder under the root folder of your application. In this case, you can set the image URL like this (assuming that the folder that the images are located in is named img
):
var imgUrl = "~/img/";
if (fileExt == ".jpeg" || fileExt == ".jpg")
imgUrl += strName + "/" + strFolder + "/" + Imgdocname;
else
imgUrl += "doc/Documents/image.jpeg";
docimg.ImageUrl = imgUrl;
docimg.Visible = true;
The ~
is a placeholder for the root of your project on the server so that the link will work no matter if your application is installed at the root of a website or somewhere below.
Upvotes: 1
Reputation: 28771
You cannot specify absolute path from any drive on computer. The ImageUrl
is not the computer's path but a Url path present in your project.
Make a folder Images
in your project root directory and specify path as
docimg.ImageUrl = "~/Images/image.jpeg";
The tilde ~ refers to root directory of project.
Upvotes: 0
Reputation: 23
IS Search your Project Name?
If Yes then use
if (fileExt == ".jpeg" || fileExt == ".jpg")
{
docimg.ImageUrl= "~/" + strName + "/" + strFolder + "/" + Imgdocname;
docimg.Visible = true;
}
else
{
docimg.ImageUrl = "~/doc/Documents/image.jpeg";
docimg.Visible=true;
}
Upvotes: 1