RandomUser
RandomUser

Reputation: 1853

Image not getting displayed mvc 4

I am storing the physical path of pictures in a table and I am trying to display the image in a view.

I am fetching it and storing it in a Model and storing it in a string property before sending it to a strongly typed view.

I am trying to display the image using the following code:

<img src="@Url.Content(@Model.imageUrl)" alt="Image"/>

If I view the source of the page the path is correctly getting loaded as shown below, but the image is not getting displayed.

<img src="C:\Documents and Settings\dinesh.sekaran\Desktop\My 
  Files\OnlineShoppingSystemMVC\OnlineShoppingSystemMVC\Resources\iphone5s.jpg"
  alt="Image"/>

Thanks for any help.

Upvotes: 0

Views: 1213

Answers (3)

JotaBe
JotaBe

Reputation: 39015

You should not expose directly a physical path of your server to your browser, and that's exactly what you're doing.

You need to convert the physical path to a virtual path.

If the path of the folder where you store the files is inside your site, you can replace part of the physical path with the virtual path, like shown here.

If not, you can always make a link to an action that returns a FileResult that contains the image. To do so you need to create an action in a controller that receives an identifier, or the path or whatever, that identifies your image, and return the image file in a FileResult.

Upvotes: 2

This path cannot be used , because it is a security issue, you have to use project folder relative path for the source adress like :

<img src="/Resources/iphone5s.jpg" alt="Image"/>

this will solve your problem.

Upvotes: 0

Oualid KTATA
Oualid KTATA

Reputation: 1116

Try to use a relative path instead and put you image in the images folder in your app. @Model.imageUrl should suffice if it's a string.

Upvotes: 0

Related Questions